package hotel.switchs.oversea.biz.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import hotel.base.oversea.constant.CallbackConstants;
import hotel.base.oversea.constant.CommonConstants;
import hotel.base.oversea.constant.CommonUrlConstants;
import hotel.base.oversea.constant.enums.*;
import hotel.base.oversea.dto.meituan.*;
import hotel.base.oversea.entity.master.CompareHotelBasicRoom;
import hotel.base.oversea.service.impl.BaseServiceImpl;
import hotel.base.oversea.utils.*;
import hotel.base.oversea.vo.CreateRoomResultVo;
import hotel.base.oversea.vo.meituan.MtHotelRoomDeatilVo;
import hotel.base.oversea.vo.meituan.MtHotelRoomDownVo;
import hotel.base.oversea.vo.meituan.request.goods.ProductFailureDetailVo;
import hotel.base.oversea.vo.meituan.request.goods.ProductFailureVo;
import hotel.base.oversea.vo.meituan.response.goods.MtHotelGoodsCancelRuleVo;
import hotel.base.oversea.vo.meituan.response.goods.MtHotelGoodsInventoryVo;
import hotel.switchs.oversea.api.VO.RoomInfoVo;
import hotel.switchs.oversea.api.VO.RoomStaticInfosVo;
import hotel.switchs.oversea.api.VO.request.*;
import hotel.switchs.oversea.api.dto.SwitchCreateRoomDto;
import hotel.switchs.oversea.api.dto.SwitchSaleRoomAboutIdsDto;
import hotel.switchs.oversea.api.entity.*;
import hotel.switchs.oversea.biz.config.service.RedisService;
import hotel.switchs.oversea.biz.mapper.*;
import hotel.switchs.oversea.biz.service.SwitchBasicRoomTypeService;
import hotel.switchs.oversea.biz.service.SwitchCreateBasicRoomService;
import hotel.switchs.oversea.biz.service.SwitchCreateRoomService;
import hotel.switchs.oversea.biz.service.SwitchSubHotelInfoService;
import hotel.switchs.oversea.biz.utils.SwitchsHttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.client.RestTemplate;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author lmf
 * @Date 2020/6/10 14:40
 **/
@Slf4j
@Service
public class SwitchCreateRoomServiceImpl extends BaseServiceImpl<SwitchCreateRoomEntity> implements SwitchCreateRoomService {
    @Autowired
    private SwitchRestTempaleUtilsService switchRestTempaleUtilsService;
    @Autowired
    private RedisService redisService;

    @Autowired
    private RestTemplate restTemplate;

    private static final String COMPAER_URL = CommonUrlConstants.COMPARE_HOST + "/";

    private static final String MEITUAN_URL = CommonUrlConstants.MEITUAN_HOST + "/";

    @Autowired
    private SwitchBasicRoomTypeService switchBasicRoomTypeService;


    @Autowired
    private SwitchCreateRoomMapper switchCreateRoomMapper;

    @Autowired
    private SwitchRoomOnlineOfflineLogMapper switchRoomOnlineOfflineLogMapper;

    @Autowired
    private SwitchHotelInfoMapper switchHotelInfoMapper;

    @Autowired
    private SwitchCreateBasicRoomService switchCreateBasicRoomService;

    @Autowired
    private SwitchCreateSubHotelMapper switchCreateSubHotelMapper;

    @Autowired
    private SwitchPushRoomDataMapper switchPushRoomDataMapper;
    @Autowired
    private SwitchRoomFailureLogMapper switchRoomFailureLogMapper;

    @Autowired
    private SwitchSubHotelInfoService switchSubHotelInfoService;

    @Autowired
    private SwitchHotelUpLogMapper switchHotelUpLogMapper;

    @Autowired
    private SwitchHotelUpMapper switchHotelUpMapper;


    @Value("${profile.type}")
    private String profile;

    @Autowired
    private SwitchCreateRoomObserveMapper switchCreateRoomObserveMapper;


    /**
     * @author: lmf
     * @date: 2020/9/14 14:00
     * 保存售卖房型的取消规则
     */
    @Autowired
    private SwitchSetRoomSaleRuleMapper switchSetRoomSaleRuleMapper;
    /**
     * @author: lmf
     * @date: 2020/9/14 14:01
     * 获取单独需要配置的酒店列表
     */
    @Autowired
    private SwitchCancelRuleSetMapper switchCancelRuleSetMapper;

    /**
     *
     * @author: lmf
     * @time: 2020/10/28 13:50
     * 美团产品通知记录表
     */
    @Autowired
     private SwitchReceiveGoodsLogMapper switchReceiveGoodsLogMapper;

     /**
      *
      * @author: lmf
      * @time: 2020/10/28 13:50
      * 记录产品失效记录表
      */
     @Autowired
     private SwitchCreateRoomFailureMapper switchCreateRoomFailureMapper;



    /**
     * 保存创建子房型实体信息
     * key 子酒店id+子物理房型id value 创建信息
     */
    private Map<String, SwitchCreateBasicRoomEntity> basicRoomMap = new HashMap<>();

    /**
     * 获取美团id
     * key 为母酒店id+母物理房型id value 美团id
     */
    private Map<String, String> mtRoomIdMap = new HashMap<>();

    /**
     * 获取效验的房型数据
     * key:携程母物理房型id+美团房型id value 效验的房型数据
     */
    private Map<String, CompareHotelBasicRoom> compareHotelBasicRoomMap = new HashMap<>();

    @Autowired
    private SwitchCreateRoomLogMapper switchCreateRoomLogMapper;


    /**
     * 保存创建售卖房型信息接口
     * key 美团产品id value 创建售卖房型信息接口
     */
    private Map<String, SwitchCreateRoomEntity> stringSwitchCreateRoomEntityMap = new HashMap<>();

    /**
     * @author: lmf
     * @date: 2020/9/19 16:13
     * 创建售卖房型
     * hotelId 子酒店id
     * basicRoomTypeId 子物理房型id
     */
    @Override
    public Result<Object>  createRoom(Integer hotelId, Integer basicRoomTypeId) {
        //添加过滤
        Example example=new Example(SwitchHotelUpEntity.class);
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("hotelId",hotelId);
        //黑名单的酒店不进行创建,不存在上架表的不创建
        SwitchHotelUpEntity switchHotelUpEntity=switchHotelUpMapper.selectOneByExample(example);
        if(ObjectUtil.isEmpty(switchHotelUpEntity)||switchHotelUpEntity.getUp()==2||switchHotelUpEntity.getStatus()==2){
            return new Result<>();
        }
        log.info("============创建售卖房型开始======================");
        //1.根据子酒店id+子物理房型id 获取到母酒店id+母物理房型房型id
        SwitchCreateBasicRoomEntity switchCreateBasicRoomEntity=switchCreateBasicRoomService.getCreateBasicRoom(hotelId, basicRoomTypeId);
        if (ObjectUtils.isEmpty(switchCreateBasicRoomEntity)) {
            log.info("======获取不到创建子物理房型的数据,子酒店id为{},子物理房型为{}", hotelId, basicRoomTypeId);
            return new Result<>("获取不到创建子物理房型的数据");
        }
        //2.根据母酒店id+母物理房型id 从房型匹配表获取美团的酒店id+美团的房型id
        String mtRoomId = "";
        Map<String, Integer> params = new HashMap<>();
        //携程母物理房型id
        params.put("roomId", switchCreateBasicRoomEntity.getMasterBasicRoomId());
        //母酒店id
        params.put("masterHotelId", switchCreateBasicRoomEntity.getMasterHotelId());
        params.put("line",2);
        //3.根据母酒店id+根据母物理房型id 获取对应的美团的酒店id + 美团的房型id
        String url = COMPAER_URL + CompareRequestUrlEnums.GET_HOTEL_BASIC_ROOM_INFO.getUrl();
        CompareHotelBasicRoom compareHotelBasicRoom;
        try {
            Result<CompareHotelBasicRoom> result = switchRestTempaleUtilsService.sendPostRequestByGetSwitchHotelIdAndRoomId(url, params);
            if (result.getCode() == CommonConstants.SUCCESS) {
                log.info("=====请求获取效验房型控制器成功,美团的房型id为:{}=======", result.getData());
                compareHotelBasicRoom=result.getData();
            } else {
                return new Result<>("调用效验房型接口获取美团的房型id失败", CreateBasicRoomStatusEnums.CATCH.getCode());
            }
        } catch (Exception e) {
            return new Result<>("调用效验房型接口获取美团的房型id失败", CreateBasicRoomStatusEnums.CATCH.getCode());
        }
        if(ObjectUtils.isEmpty(compareHotelBasicRoom)){
            log.info("======找不到携程和美团房型的映射数据=============");
            return new Result<>("找不到携程和美团房型的映射数据");
        }
        mtRoomId=compareHotelBasicRoom.getMtHotelRoomId();
        log.info("======获取创建子物理房型的数据,子酒店id为{},子物理房型为{},美团的房型id为{}", hotelId, basicRoomTypeId,mtRoomId);
        String mtHotelId = compareHotelBasicRoom.getMtHotelId();
        //4.根据美团酒店id查询该酒店下的产品列表
        url = MEITUAN_URL + MtRequestUrlEnums.GET_MT_GOODS_BY_HOTEL_ID.getUrl();
        Map<String, Integer> param = new HashMap<>();
        param.put("hotelId", Integer.valueOf(mtHotelId));
        Map<String, List<MtHotelGoodsDto>> goodsMap = new HashMap<>();
        Result result;
        try {
            result = switchRestTempaleUtilsService.sendPostRequest(url, param);
            if (result.getCode() != CommonConstants.SUCCESS) {
                log.info("==========获取美团的产品列表数据失败,错误原因是{}=============", result.getMsg());
                return new Result<>("获取美团的产品列表数据失败", CreateBasicRoomStatusEnums.CATCH.getCode());
            } else {
                if (!ObjectUtils.isEmpty(result.getData())) {
                    goodsMap = (Map<String, List<MtHotelGoodsDto>>) result.getData();
                }
            }
        } catch (Exception e) {
            log.info("===============获取美团的产品列表数据，产生的异常为{}", e);
            return new Result<>("获取美团的产品列表数据失败", CreateBasicRoomStatusEnums.CATCH.getCode());
        }
        log.info("=============获取美团的产品数据成功,长度为{}================", goodsMap.size());
        //4.找到对应房型的产品
        List<MtHotelGoodsDto> goodsDto = new ArrayList<>();
        if (goodsMap.containsKey(mtRoomId)) {
            goodsDto = goodsMap.get(mtRoomId);
        }
        log.info("======房型为mtRoomId{},对应的产品列表为{}",mtRoomId, JSON.toJSONString(goodsDto));
        log.info("============查找美团房型id为{}的产品信息,產品的長度為{}",mtRoomId,goodsDto.size());
        if (ObjectUtils.isEmpty(goodsDto) || goodsDto.size() < 1) {
            log.info("========该房型的产品信息暂无==============");
            return new Result<>("该房型的产品信息暂无", CreateBasicRoomStatusEnums.NO_GOODS.getCode());
        }
        //5.封装创建售卖房型的请求数据
        int num = 0;
        CreateRoomVo createRoomVo;
        List<Integer> rateCodePropertyValueIDList;
        int breakFast;
        Map<String, Integer> map;
        SwitchLableEnums switchLableEnums;
        SwitchCreateRoomEntity switchCreateRoomByMt;
        Result<CreateRoomResultVo> resultRoom;
        SwitchCreateRoomEntity switchCreateRoomEntity;
        SwitchCreateRoomLogEntity switchCreateRoomLogEntity;
        CreateRoomResultVo createRoomResultVo;
        CompareHotelBasicRoom compareHotelBasicRoomInfo;
        switchTab:for (MtHotelGoodsDto mtHotelGoodsDto : goodsDto) {
            //如果产品是钟点房 不创建 房态是不可预定(推送的话就会做判断)
            //過濾掉不可展示的產品 mtHotelGoodsDto.getMtGoodsStatus()==3
            log.info("============产品的状态值为{},产品的goodid为{}=======",mtHotelGoodsDto.getMtGoodsStatus(),mtHotelGoodsDto.getMtGoodsId());
            if(mtHotelGoodsDto.getMtGoodsStatus()==3){
                continue switchTab;
            }
            //过滤钟点房
            if (mtHotelGoodsDto.getMtGoodsType() ==2) {
                continue switchTab;
            }
            //过滤掉需要身份证验证的
            if(mtHotelGoodsDto.getMtNeedIdentityCard()==1){
                continue switchTab;
            }
            /**
             * 过滤掉需要连住的产品
             * 如果最少入住天数>1 产品暂时不要
             * 如果最少预定间数>1 产品暂时不要
             */
            List<MtHotelGoodsBookRuleDto> mtHotelGoodsBookRuleDtos=mtHotelGoodsDto.getBookRuleList();
            if(!ObjectUtil.isEmpty(mtHotelGoodsBookRuleDtos)&&mtHotelGoodsBookRuleDtos.size()>0){
                for(MtHotelGoodsBookRuleDto mtHotelGoodsBookRuleDto:mtHotelGoodsBookRuleDtos){
                    if(mtHotelGoodsBookRuleDto.getMtSerialCheckinMin()>1||mtHotelGoodsBookRuleDto.getMtRoomCountMin()>1){
                        continue switchTab;
                    }
                }
            }
            //过滤掉第三方开票方
            MtHotelGoodsInvoiceDto mtHotelGoodsInvoiceDto=mtHotelGoodsDto.getInvoice();
            if(!ObjectUtils.isEmpty(mtHotelGoodsInvoiceDto)){
               if(mtHotelGoodsInvoiceDto.getInvoiceMode()==3||mtHotelGoodsInvoiceDto.getInvoiceMode()==2){
                   continue switchTab;
               }
            }
            //过滤掉产品名称需要证件的产品
            if(mtHotelGoodsDto.getMtGoodsName().contains("证件")){
                continue switchTab;
            }
            if(mtHotelGoodsDto.getMtGoodsName().contains("学生证")){
                continue switchTab;
            }
            if(mtHotelGoodsDto.getMtGoodsName().contains("护士证")){
                continue switchTab;
            }
            if(mtHotelGoodsDto.getMtGoodsName().contains("资格证")){
                continue switchTab;
            }
            if(mtHotelGoodsDto.getMtGoodsName().contains("户籍")){
                continue switchTab;
            }
            if(mtHotelGoodsDto.getMtGoodsName().contains("零点后入住")){
                continue switchTab;
            }
            //房型名称和产品名称的相识度百分百
      /*      if(CompareUtil.getSimilarityRatio(compareHotelBasicRoom.getMtRoomName().replaceAll(" ","").replaceAll(",",""),mtHotelGoodsDto.getMtGoodsName().replaceAll(" ","").replaceAll(",",""))!=100.0f){
                //过滤产品名称+房型名称不匹配的数据  产品名称包含房型名称
                if(!mtHotelGoodsDto.getMtGoodsName().contains(compareHotelBasicRoom.getMtRoomName())){
                    //房型名称包含产品名称
                    if(!compareHotelBasicRoom.getMtRoomName().contains(mtHotelGoodsDto.getMtGoodsName())){
                        SwitchCreateRoomObserveEntity switchCreateRoomObserveEntity=new SwitchCreateRoomObserveEntity();
                        switchCreateRoomObserveEntity.setCreateTime(new Date());
                        switchCreateRoomObserveEntity.setHotelId(hotelId);
                        switchCreateRoomObserveEntity.setId(StringUtil.getUUID());
                        switchCreateRoomObserveEntity.setMasterHotelId(switchCreateBasicRoomEntity.getMasterHotelId());
                        switchCreateRoomObserveEntity.setMtRoomName(compareHotelBasicRoom.getMtRoomName());
                        switchCreateRoomObserveEntity.setModifyTime(new Date());
                        switchCreateRoomObserveEntity.setMtGoodId(Integer.valueOf(mtHotelGoodsDto.getMtGoodsId()));
                        switchCreateRoomObserveEntity.setMtGoodName(mtHotelGoodsDto.getMtGoodsName());
                        switchCreateRoomObserveEntity.setMtHotelId(Integer.valueOf(mtHotelGoodsDto.getMtHotelId()));
                        switchCreateRoomObserveEntity.setMtRoomId(mtHotelGoodsDto.getMtRoomId());
                        switchCreateRoomObserveEntity.setRemark("产品名称跟房型名称不符不进行创建");
                        switchCreateRoomObserveEntity.setState(0);
                        switchCreateRoomObserveMapper.insert(switchCreateRoomObserveEntity);
                        continue switchTab;
                    }
                }
            }*/
            //封装调用创建售卖房型接口请求参数
            createRoomVo = new CreateRoomVo();
            //5.1 设置售卖房型信息
            //设置子酒店
            createRoomVo.setHotelId(hotelId);
            //设置子物理房型id
            createRoomVo.setBasicRoomTypeId(basicRoomTypeId);
            //5.2.对产品名称进行拆分 把餐食规则 入住人数 分出来
            rateCodePropertyValueIDList = new ArrayList<>();
            synchronized (this){
                map = getBreakFastId(mtHotelGoodsDto.getBreakfastList());
            }
            Map<String, Integer> map2=new HashMap<>();
            synchronized (this){
                map2 = getBreakFastId(mtHotelGoodsDto.getBreakfastList());
            }
            breakFast = map.get("breakId");
            int breakFast2=map2.get("breakId");
            int meanCount=map.get("mealCount");
            //餐食做二次效验
            if(breakFast!=breakFast2){
                continue switchTab;
            }
            log.info("==========子物理房型为{}根据默认的早餐规则获取的餐食类型为{}",basicRoomTypeId,breakFast);
            if (breakFast == 8) {
                rateCodePropertyValueIDList.add((breakFast < 1) ? SwitchBreakFastEnums.NO.getCode() : breakFast);
                createRoomVo.setMealCount(map.get("mealCount"));
            } else {
                rateCodePropertyValueIDList.add((breakFast < 1) ? SwitchBreakFastEnums.NO.getCode() : breakFast);
            }
            //5.3添加促销标签 随机获取某个标签
            switchLableEnums = RandomEnums.random(SwitchLableEnums.class);
            rateCodePropertyValueIDList.add(switchLableEnums.getCode());
            createRoomVo.setRateCodePropertyValueIDList(rateCodePropertyValueIDList.stream().toArray(Integer[]::new));
            //如果美團的產品id+酒店id有對應的映射過濾
            switchCreateRoomByMt = switchCreateRoomMapper.selectByMtGoodsIdAndHotelId(Integer.valueOf(mtHotelGoodsDto.getMtHotelId()), Integer.valueOf(mtHotelGoodsDto.getMtGoodsId()));
            if(!ObjectUtils.isEmpty(switchCreateRoomByMt)){
                log.info("========美团的酒店id为{},美团的产品id为{}=======",mtHotelGoodsDto.getMtHotelId(),mtHotelGoodsDto.getMtGoodsId());
                log.info("==============更新售賣房型的規則switchCreateRoomByMt的值为{}=======================",switchCreateRoomByMt.toString());
                setRoomSaleRule2(hotelId, switchCreateRoomByMt.getRoomId(), switchCreateRoomByMt.getRoomName(), mtHotelGoodsDto.getCancelRuleList(), mtHotelGoodsDto.getBookRuleList());
                continue switchTab;
            }
            //判断创建物理房型表是否有数据
            SwitchBasicRoomType switchBasicRoomType=switchBasicRoomTypeService.selectByBasicId(hotelId,basicRoomTypeId);
            if(ObjectUtil.isEmpty(switchBasicRoomType)){
                log.info("==========不存在此物理房型数据=============");
                continue switchTab;
            }
            //到效验项目获取房型映射信息
            String url2=CommonUrlConstants.COMPARE_HOST+CompareRequestUrlEnums.GET_HOTEL_BASIC_ROOM_INFO_BY_SIWTCH_ROOM_ID_AND_MT_ROOM_ID.getUrl();
            Map<String,Integer> params2=new HashMap<>();
            params2.put("switchRoomId",switchBasicRoomType.getMasterBasicRoomId());
            params2.put("mtRoomId",Integer.valueOf(mtHotelGoodsDto.getMtRoomId()));
            Result<CompareHotelBasicRoom> result2=switchRestTempaleUtilsService.sendPostRequestToCompareToGetRoomInfo(url2,params2);
            if(result2.getCode()!=CommonConstants.SUCCESS){
                continue switchTab;
            }
            compareHotelBasicRoomInfo=result2.getData();
            //美团房型和携程房型映射不上不进行创建
            if(ObjectUtil.isEmpty(compareHotelBasicRoomInfo)){
                continue switchTab;
            }
            //判断是否是同个房型下,如果不是不进行创建
            if(!mtRoomId.equals(mtHotelGoodsDto.getMtRoomId())){
                log.info("=========美团的房型id为{}和产品的房型id{},不一样不进行创建==",mtRoomId,mtHotelGoodsDto.getMtRoomId());
                continue switchTab;
            }
            //7.调用创建接口
            resultRoom = switchBasicRoomTypeService.createRoom(createRoomVo);
            if (resultRoom.getCode() == CommonConstants.SUCCESS) {
                createRoomResultVo = resultRoom.getData();
                if(createRoomResultVo.getResultStatusInfoVo().getResultMsg().contains("基础房型和RateCode组合已经存在")){
                    //如果售賣房型id+子酒店id 創建過跳过
                    SwitchCreateRoomEntity switchCreateRoom = switchCreateRoomMapper.selectByRoomIdAndHotelId(hotelId, createRoomResultVo.getRoomTypeId());
                    if(!ObjectUtils.isEmpty(switchCreateRoom)){
                        log.info("============该售卖房型已经存在====================");
                        continue switchTab;
                    }
                }
                //如果售卖房型id存在就跳过
                SwitchCreateRoomEntity switchCreateRoom = switchCreateRoomMapper.selectByRoomIdAndHotelId(hotelId, createRoomResultVo.getRoomTypeId());
                if(!ObjectUtils.isEmpty(switchCreateRoom)){
                    log.info("==============该售卖房型已经存在================");
                    continue switchTab;
                }
                if(StringUtil.isBlank(createRoomResultVo.getRoomName())){
                    //从失效表获取售卖房型名称
                    SwitchCreateRoomFailureEntity switchCreateRoomFailureEntity=switchCreateRoomFailureMapper.selectRoomNameByHotelIdAndRoomId(hotelId,createRoomResultVo.getRoomTypeId());
                    if(!ObjectUtil.isEmpty(switchCreateRoomFailureEntity)){
                        createRoomResultVo.setRoomName(switchCreateRoomFailureEntity.getRoomName());
                    }else{//获取静态信息获取
                        //获取母酒店id
                        Map<String,Object> map1=new HashMap<>();
                        map1.put("hotelId",hotelId);
                        String[] types=new String[]{"RoomTypeSimpleEntity"};
                        map1.put("returnDataTypeList",types);
                        String strPro = SwitchsHttpUtils.sendRequest(map1, "gethotelroomstaticinfo", profile);
                        //对结构进行格式化
                        RoomStaticInfosVo roomStaticInfosVo = null;
                        Map<String,Object> parse = JacksonUtil.parse(strPro,Map.class);
                        String retJson = JacksonUtil.objectToJson(parse.get("roomStaticInfos"));
                        roomStaticInfosVo =JacksonUtil.parse(retJson, RoomStaticInfosVo.class);
                        List<RoomInfoVo>  roomInfoVos=roomStaticInfosVo.getRoomStaticInfo().get(0).getRoomInfos().getRoomInfo();
                        if(!ObjectUtils.isEmpty(roomInfoVos)&&roomInfoVos.size()>0){
                            for(RoomInfoVo roomInfoVo:roomInfoVos) {
                                if (roomInfoVo.getRoomId().equals(createRoomResultVo.getRoomTypeId())) {
                                    createRoomResultVo.setRoomName(roomInfoVo.getRoomName());
                                }
                            }
                        }
                    }
                }
                log.info("===美团的酒店id为{},房型id为{},产品id为{},携程的产品id为{},餐食为{}===",mtHotelId,mtRoomId,mtHotelGoodsDto.getMtGoodsId(),resultRoom.getData().getRoomTypeId(),breakFast);
                //封装创建售卖房型数据
                switchCreateRoomEntity = new SwitchCreateRoomEntity();
                switchCreateRoomEntity.setId(StringUtil.getUUID());
                switchCreateRoomEntity.setMtRoomName(compareHotelBasicRoom.getMtRoomName());
                switchCreateRoomEntity.setSwitchRoomName(compareHotelBasicRoom.getSwitchRoomName());
                switchCreateRoomEntity.setHotelId(hotelId);
                switchCreateRoomEntity.setBasicRoomTypeId(basicRoomTypeId);
                switchCreateRoomEntity.setRoomTypeId(0);
                switchCreateRoomEntity.setMasterHotelId(switchCreateBasicRoomEntity.getMasterHotelId());
                switchCreateRoomEntity.setRelationRoomId(0);
                switchCreateRoomEntity.setRateCodePropertyValueIdList(StringUtil.getStringByList(rateCodePropertyValueIDList, ","));
                switchCreateRoomEntity.setMealCount(meanCount);
                switchCreateRoomEntity.setApplicabilityInfo(createRoomVo.getApplicabilityInfo());
                switchCreateRoomEntity.setApplicabilityList(StringUtil.getStringByArray(createRoomVo.getApplicabilityList(), ","));
                switchCreateRoomEntity.setPriceSystemName(ObjectUtils.isEmpty(createRoomVo.getPriceSystemName()) ? "" : createRoomVo.getPriceSystemName());
                switchCreateRoomEntity.setBlackChannelList(StringUtil.getStringByArray(createRoomVo.getBlackChannelList(), ","));
                switchCreateRoomEntity.setMtHotelId(Integer.valueOf(mtHotelId));
                switchCreateRoomEntity.setMtGoodId(Integer.valueOf(mtHotelGoodsDto.getMtGoodsId()));
                switchCreateRoomEntity.setState(0);
                switchCreateRoomEntity.setUp(0);
                switchCreateRoomEntity.setRemark("");
                switchCreateRoomEntity.setCreateTime(new Date());
                switchCreateRoomEntity.setMtGoodName(mtHotelGoodsDto.getMtGoodsName());
                switchCreateRoomEntity.setModifyTime(new Date());
                switchCreateRoomEntity.setMtRoomId(mtHotelGoodsDto.getMtRoomId());
                switchCreateRoomEntity.setRoomId(createRoomResultVo.getRoomTypeId());
                switchCreateRoomEntity.setRoomName(createRoomResultVo.getRoomName());
                switchCreateRoomLogEntity=new SwitchCreateRoomLogEntity();
                switchCreateRoomLogEntity.setBasicId(switchCreateBasicRoomEntity.getMasterBasicRoomId());
                switchCreateRoomLogEntity.setCreateTime(new Date());
                switchCreateRoomLogEntity.setMtGoodSingle(JSON.toJSONString(goodsDto));
                switchCreateRoomLogEntity.setMtRoomId(Integer.valueOf(mtRoomId));
                switchCreateRoomLogEntity.setRoomId(createRoomResultVo.getRoomTypeId());
                switchCreateRoomLogEntity.setMtGoodTotal(JSON.toJSONString(goodsMap));
                if(createRoomResultVo.getRoomTypeId()<1){
                    continue switchTab;
                }
                //同个产品对应的产品列表id过就无需入库
                SwitchCreateRoomEntity switchCreateRoomEntity2 = switchCreateRoomMapper.selectByRoomIdAndHotelIdAndGoodsID(hotelId, createRoomResultVo.getRoomTypeId(), mtHotelGoodsDto.getMtGoodsId(),mtHotelGoodsDto.getMtHotelId());
                if (!ObjectUtils.isEmpty(switchCreateRoomEntity2)) {
                    num++;
                    continue switchTab;
                }else {
                    String key = CommonConstants.SWITCH_SELLING_THE_HOUSE_TYPE + switchCreateRoomEntity.getMtHotelId() + "_" + switchCreateRoomEntity.getMtGoodId();
                    redisService.set(key,switchCreateRoomEntity.getMtRoomId());
                    if(breakFast!=498160){
                        String key2=CommonConstants.SWITCH_CREATE_ROOM_BREAKFAST+ mtHotelId + "_" + mtHotelGoodsDto.getMtGoodsId()+"_1";
                        redisService.set(key2,meanCount);
                    }else{
                        String key2=CommonConstants.SWITCH_CREATE_ROOM_BREAKFAST+ mtHotelId + "_" + mtHotelGoodsDto.getMtGoodsId()+"_0";
                        redisService.set(key2,0);
                    }
                    //删除旧的售卖房型映射数据
                    String key3=CommonConstants.SWITCH_SELLING_REDIS_BY_MT+mtHotelId+"_"+mtHotelGoodsDto.getMtGoodsId();
                    if(redisService.exists(key3)){
                        redisService.remove(key3);
                    }
                    //删除旧的美团酒店的单日产品价格 和多日产品价格缓存
                    int day=3;
                    //删除价格和房态缓存
                    for(int i=0;i<day;i++){
                        //删除房态
                        key=CallbackConstants.REDIS_KEY_CALLBACK_CHANGE_STATUS_HEAD+mtHotelId+"_"+mtHotelGoodsDto.getMtGoodsId()+"_"+DateUtil.getDateAfter(new Date(),i);
                        if(redisService.exists(key)){
                            redisService.remove(key);
                        }
                        //删除价格
                        key=CallbackConstants.REDIS_KEY_CALLBACK_CHANGE_PRICE_HEAD+mtHotelId+"_"+mtHotelGoodsDto.getMtGoodsId()+"_"+DateUtil.getDateAfter(new Date(),i);
                        if(redisService.exists(key)){
                            redisService.remove(key);
                        }
                    }
                    //删除缓存 美团判断3天数据是否一样，不一样就针对单天进行比较，需同时清除这俩个数据
                    key=CallbackConstants.REDIS_ROW_GOODS_PRICE_CHECK+mtHotelId+"_"+mtHotelGoodsDto.getMtGoodsId();
                    if(redisService.exists(key)){
                        log.info("======key {}删除中=========",key);
                        redisService.remove(key);
                    }
                    key=CallbackConstants.REDIS_ROW_GOODS_STATUS_CHECK+mtHotelId+"_"+mtHotelGoodsDto.getMtGoodsId();
                    if(redisService.exists(key)){
                        log.info("======key {}删除中=========",key);
                        redisService.remove(key);
                    }
                    //删除取消规则 SWITCH_CREATE_ROOM_CANCEL_RULE
                    key=CommonConstants.SWITCH_CREATE_ROOM_CANCEL_RULE+mtHotelId+"_"+mtHotelGoodsDto.getMtGoodsId()+"_1";
                    if(redisService.exists(key)){
                        redisService.remove(key);
                    }
                    key=CommonConstants.SWITCH_CREATE_ROOM_CANCEL_RULE+mtHotelId+"_"+mtHotelGoodsDto.getMtGoodsId()+"_0";
                    if(redisService.exists(key)){
                        redisService.remove(key);
                    }
                    //设置取消规则
                    List<MtHotelGoodsCancelRuleDto> mtHotelGoodsCancelRuleDtos=mtHotelGoodsDto.getCancelRuleList();
                    if(ObjectUtils.isEmpty(mtHotelGoodsCancelRuleDtos)||mtHotelGoodsCancelRuleDtos.size()<1){
                        key=CommonConstants.SWITCH_CREATE_ROOM_CANCEL_RULE+mtHotelId+"_"+mtHotelGoodsDto.getMtGoodsId()+"_0";
                        redisService.set(key,"0");
                    }else{
                        for(MtHotelGoodsCancelRuleDto mtHotelGoodsCancelRuleVo:mtHotelGoodsCancelRuleDtos){
                            //不可取消
                            if(mtHotelGoodsCancelRuleVo.getMtCancelType()==0){
                                key=CommonConstants.SWITCH_CREATE_ROOM_CANCEL_RULE+mtHotelId+"_"+mtHotelGoodsDto.getMtGoodsId()+"_0";
                                redisService.set(key,"0");
                            }else{
                                key=CommonConstants.SWITCH_CREATE_ROOM_CANCEL_RULE+mtHotelId+"_"+mtHotelGoodsDto.getMtGoodsId()+"_1";
                                String time="";
                                time=mtHotelGoodsCancelRuleVo.getMtAheadCancelDays()+"_"+mtHotelGoodsCancelRuleVo.getMtAheadCancelHours();
                                redisService.set(key,time);
                            }
                        }
                    }
                    switchCreateRoomMapper.insert(switchCreateRoomEntity);
                    switchCreateRoomLogMapper.insert(switchCreateRoomLogEntity);
                    //设置房态库存共享
                    setRoomShare(hotelId, createRoomResultVo.getRoomTypeId(), SwitchChannelNameEnums.CTRIP.getName());
                    setRoomShare(hotelId, createRoomResultVo.getRoomTypeId(), SwitchChannelNameEnums.B2B.getName());
                    //如果是新产品就无需调用
                    Example example1=new Example(SwitchCreateRoomFailureEntity.class);
                    Example.Criteria criteria1=example1.createCriteria();
                    criteria1.andEqualTo("roomId",createRoomResultVo.getRoomTypeId());
                    List<SwitchCreateRoomFailureEntity> list=switchCreateRoomFailureMapper.selectByExample(example1);
                    if(!ObjectUtils.isEmpty(list)||list.size()>1){
                        SetRoomOnlineOfflineVo setRoomOnlineOfflineVo=new SetRoomOnlineOfflineVo();
                        setRoomOnlineOfflineVo.setRoomTypeId(createRoomResultVo.getRoomTypeId());
                        setRoomOnlineOfflineVo.setHotelId(hotelId);
                        setRoomOnlineOfflineVo.setChannel("Ctrip");
                        setRoomOnlineOfflineVo.setRoomStatus(1);
                        switchBasicRoomTypeService.setRoomOnlineOffline(setRoomOnlineOfflineVo);
                    }
                   // setRoomOnlineOfflineVo.setChannel("Qunar");
                   // switchBasicRoomTypeService.setRoomOnlineOffline(setRoomOnlineOfflineVo);
                    //设置房型售卖规则
                    log.info("============获取产品的取消规则{}====================", mtHotelGoodsDto.getCancelRuleList().size());
                    setRoomSaleRule(hotelId, createRoomResultVo.getRoomTypeId(), switchCreateRoomEntity.getRoomName(), mtHotelGoodsDto.getCancelRuleList(), mtHotelGoodsDto.getBookRuleList());
                    num++;
                    log.info("=======创建售卖房型完成===============");
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            } else {
                log.info("=======创建售卖房型失败===============,原因{}", result.getMsg());
                return new Result<>(result.getMsg());
            }
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        if (num == goodsDto.size()) {
            return new Result<>();
        } else {
            return new Result<>(CreateBasicRoomStatusEnums.LITTER.getMessage(), CreateBasicRoomStatusEnums.LITTER.getCode());
        }
    }
    /**
     * 效验是否有库存
     * 没有就不进行创建售卖房型
     *
     * @param mtHotelGoodsDto
     */
    public boolean isCanCreateRoom(MtHotelGoodsDto mtHotelGoodsDto) {
        Map<String, Object> param = new HashMap<>();
        param.put("roomId", mtHotelGoodsDto.getMtRoomId());
        param.put("goodsId", mtHotelGoodsDto.getMtGoodsId());
        param.put("days", 1);
        MtHotelGoodsInventoryVo mtHotelGoodsInventoryVo = getMtGoodsInventory(param);
        log.info("===============美团返回的库存为{}", mtHotelGoodsInventoryVo.toString());
        //产品是可预订且有库存就进行创建
        if (mtHotelGoodsInventoryVo.getRemain() > 0 && mtHotelGoodsDto.getMtGoodsStatus() == 1) {
            return true;
        }
        return false;
    }

    /**
     * 获取美团的库存
     */
    public MtHotelGoodsInventoryVo getMtGoodsInventory(Map<String, Object> params) {
        MtHotelGoodsInventoryVo mtHotelGoodsInventoryVo = new MtHotelGoodsInventoryVo();
        String url = MEITUAN_URL + MtRequestUrlEnums.GET_MT_GOODS_INVENTORY_BY_DAYS.getUrl();
        try {
            Result<MtHotelGoodsInventoryVo> result2 = switchRestTempaleUtilsService.sendPostRequestGetGoodsInventory(url, params);
            if (result2.getCode() == CommonConstants.SUCCESS) {
                mtHotelGoodsInventoryVo = result2.getData();
                log.info("库存返回成功,返回的数据是{}", mtHotelGoodsInventoryVo);
            } else {
                log.info("=======库存返回失败=========,失败原因是{}", result2.getMsg());
            }
        } catch (Exception e) {
            log.info("发生的异常为{}", e.getMessage());
        }
        return mtHotelGoodsInventoryVo;
    }

    /**
     * 设置售卖规则: 设置房型的取消规则 默认比美团提前俩个小时
     * 比如美团是入住前18:00可取消 我们设置就往前面推移2个小时(暂时取消)
     *
     * @param hotelId        子酒店id
     * @param roomTypeId     售卖房型id
     * @param roomName       售卖房型名称
     * @param cancelRuleList 美团产品的取消规则
     * @param bookRuleList
     */
    private void setRoomSaleRule(Integer hotelId, int roomTypeId, String roomName, List<MtHotelGoodsCancelRuleDto> cancelRuleList, List<MtHotelGoodsBookRuleDto> bookRuleList) {
        //封装设置售卖规则表单结构
        SetRoomSaleRuleVo setRoomSaleRuleVo = new SetRoomSaleRuleVo();
        setRoomSaleRuleVo.setHotelId(hotelId);
        //获取酒店的星级
        Integer hotelStart = switchHotelInfoMapper.selectHotelStarByHotelId(hotelId);
        if (ObjectUtils.isEmpty(hotelStart)) {
            hotelStart = 1;
        }
        RoomTypeVo roomTypeVo = new RoomTypeVo();
        roomTypeVo.setRoomTypeId(roomTypeId);
        roomTypeVo.setRoomTypeName(roomName);
        //设置售卖房型列表
        List<RoomTypeVo> roomTypeList = new ArrayList<>();
        roomTypeList.add(roomTypeVo);
        setRoomSaleRuleVo.setRoomTypeList(roomTypeList);
        setRoomSaleRuleVo.setSetCtripDailyRule(false);
        //设置规则
        List<SaleRuleVo> saleRuleList = new ArrayList<>();
        //1.ctrip 设置取消规则
        List<SaleRuleVo> ctripSale = cancel(cancelRuleList, SwitchChannelNameEnums.CTRIP.getName(), hotelStart);
        //1.ctrip 设置预定规则
        ctripSale = book(ctripSale, bookRuleList, SwitchChannelNameEnums.CTRIP.getName());
        //去哪儿 设置取消规则
        List<SaleRuleVo> otherSale = cancel(cancelRuleList, SwitchChannelNameEnums.QUNAR.getName(), hotelStart);
        //去哪儿  设置预定时间
        otherSale = book(otherSale, bookRuleList, SwitchChannelNameEnums.QUNAR.getName());
        //b2b设置取消规则
        List<SaleRuleVo> b2bSale = cancel(cancelRuleList, SwitchChannelNameEnums.B2B.getName(), hotelStart);
        //b2b设置预定规则
        b2bSale = book(b2bSale, bookRuleList, SwitchChannelNameEnums.B2B.getName());
        //channel设置取消规则
        //List<SaleRuleVo> channelSale = cancel(cancelRuleList, SwitchChannelNameEnums.CHANNEL_A.getName(), hotelStart);
        //channel设置预定规则
       // channelSale = book(channelSale, bookRuleList, SwitchChannelNameEnums.CHANNEL_A.getName());
        for (SaleRuleVo saleRuleVo : ctripSale) {
            //携程是否设置最晚确认时间 默认0 18
           /* saleRuleVo.setSetLastConfirmTime(true);
            saleRuleVo.setLastBookDay(0);
            saleRuleVo.setLastConfirmTime(String.valueOf(15));*/
            saleRuleVo.setSetLimitVisiable(false);
            saleRuleList.add(saleRuleVo);
        }
        for (SaleRuleVo saleRuleVo : otherSale) {
            saleRuleVo.setSetLimitVisiable(false);
            saleRuleList.add(saleRuleVo);
        }
        for (SaleRuleVo saleRuleVo : b2bSale) {
            saleRuleVo.setSetLimitVisiable(false);
            saleRuleList.add(saleRuleVo);
        }
       /* for (SaleRuleVo saleRuleVo : channelSale) {
            saleRuleVo.setSetLimitVisiable(false);
            saleRuleList.add(saleRuleVo);
        }*/
        log.info("=============该产品设置的规则长度有{}==========", saleRuleList.size());
        setRoomSaleRuleVo.setSaleRuleList(saleRuleList);
        log.info("===============更新售卖规则的请求数据为{}===========", setRoomSaleRuleVo);
        //把规则记录到表里
        SwitchSetRoomSaleRuleEntity switchSetRoomSaleRuleEntity = new SwitchSetRoomSaleRuleEntity();
        switchSetRoomSaleRuleEntity.setId(StringUtil.getUUID());
        switchSetRoomSaleRuleEntity.setCreateTime(new Date());
        switchSetRoomSaleRuleEntity.setHotelId(hotelId);
        switchSetRoomSaleRuleEntity.setRoomTypeId(roomTypeId);
        switchSetRoomSaleRuleEntity.setRoomTypeName(roomName);
        switchSetRoomSaleRuleEntity.setSetStripDailyRule(setRoomSaleRuleVo.isSetCtripDailyRule());
        switchSetRoomSaleRuleEntity.setSaleRuleListJson(JSONObject.toJSONString(saleRuleList));
        Result<Object> result = switchBasicRoomTypeService.setRoomSaleRule(setRoomSaleRuleVo);
        if (result.getCode() == CommonConstants.SUCCESS) {
            switchSetRoomSaleRuleMapper.insert(switchSetRoomSaleRuleEntity);
            setRoomShare(hotelId, roomTypeId, SwitchChannelNameEnums.QUNAR.getName());
            //setRoomShare(hotelId, roomTypeId, SwitchChannelNameEnums.CHANNEL_A.getName());
            log.info("=======设置售卖规则成功====");
        } else {
            log.info("========设置售卖规则失败=====,原因是{}", result.getMsg());
        }
    }

    /**
     * 设置售卖规则: 设置房型的取消规则 默认比美团提前俩个小时
     * 比如美团是入住前18:00可取消 我们设置就往前面推移2个小时(暂时取消)
     *
     * @param hotelId        子酒店id
     * @param roomTypeId     售卖房型id
     * @param roomName       售卖房型名称
     * @param cancelRuleList 美团产品的取消规则
     * @param bookRuleList
     */
    private void setRoomSaleRule2(Integer hotelId, int roomTypeId, String roomName, List<MtHotelGoodsCancelRuleDto> cancelRuleList, List<MtHotelGoodsBookRuleDto> bookRuleList) {
        //封装设置售卖规则表单结构
        SetRoomSaleRuleVo setRoomSaleRuleVo = new SetRoomSaleRuleVo();
        setRoomSaleRuleVo.setHotelId(hotelId);
        //获取酒店的星级
        Integer hotelStart = switchHotelInfoMapper.selectHotelStarByHotelId(hotelId);
        if (ObjectUtils.isEmpty(hotelStart)) {
            hotelStart = 1;
        }
        RoomTypeVo roomTypeVo = new RoomTypeVo();
        roomTypeVo.setRoomTypeId(roomTypeId);
        roomTypeVo.setRoomTypeName(roomName);
        //设置售卖房型列表
        List<RoomTypeVo> roomTypeList = new ArrayList<>();
        roomTypeList.add(roomTypeVo);
        setRoomSaleRuleVo.setRoomTypeList(roomTypeList);
        setRoomSaleRuleVo.setSetCtripDailyRule(false);
        //设置规则
        List<SaleRuleVo> saleRuleList = new ArrayList<>();
        //1.ctrip 设置取消规则
        List<SaleRuleVo> ctripSale = cancel(cancelRuleList, SwitchChannelNameEnums.CTRIP.getName(), hotelStart);
        //ctrip 设置预定预定规则
        ctripSale = book(ctripSale, bookRuleList, SwitchChannelNameEnums.CTRIP.getName());
        //去哪儿设置取消规则
        List<SaleRuleVo> otherSale = cancel(cancelRuleList, SwitchChannelNameEnums.QUNAR.getName(), hotelStart);
        //去哪儿设置预定规则
        otherSale = book(otherSale, bookRuleList, SwitchChannelNameEnums.QUNAR.getName());
        //b2b设置取消规则
        List<SaleRuleVo> b2bSale = cancel(cancelRuleList, SwitchChannelNameEnums.B2B.getName(), hotelStart);
        //b2b设置预定规则
        b2bSale = book(b2bSale, bookRuleList, SwitchChannelNameEnums.B2B.getName());
        //channel设置取消规则
       // List<SaleRuleVo> channelSale = cancel(cancelRuleList, SwitchChannelNameEnums.CHANNEL_A.getName(), hotelStart);
        //channel设置预定规则
       // channelSale = book(channelSale, bookRuleList, SwitchChannelNameEnums.CHANNEL_A.getName());
        for (SaleRuleVo saleRuleVo : ctripSale) {
            //携程是否设置最晚确认时间 默认0 18
           /* saleRuleVo.setSetLastConfirmTime(true);
            saleRuleVo.setLastBookDay(0);
            saleRuleVo.setLastConfirmTime(String.valueOf(15));*/
            saleRuleVo.setSetLimitVisiable(false);
            saleRuleList.add(saleRuleVo);
        }
        for (SaleRuleVo saleRuleVo : otherSale) {
            saleRuleVo.setSetLimitVisiable(false);
            saleRuleList.add(saleRuleVo);
        }
        for (SaleRuleVo saleRuleVo : b2bSale) {
            saleRuleVo.setSetLimitVisiable(false);
            saleRuleList.add(saleRuleVo);
        }
      /*  for (SaleRuleVo saleRuleVo : channelSale) {
            saleRuleVo.setSetLimitVisiable(false);
            saleRuleList.add(saleRuleVo);
        }*/
        log.info("=============该产品设置的规则长度有{}==========", saleRuleList.size());
        setRoomSaleRuleVo.setSaleRuleList(saleRuleList);
        log.info("===============更新售卖规则的请求数据为{}===========", setRoomSaleRuleVo);
        //把规则记录到表里
        SwitchSetRoomSaleRuleEntity switchSetRoomSaleRuleEntity = new SwitchSetRoomSaleRuleEntity();
        switchSetRoomSaleRuleEntity.setId(StringUtil.getUUID());
        switchSetRoomSaleRuleEntity.setCreateTime(new Date());
        switchSetRoomSaleRuleEntity.setHotelId(hotelId);
        switchSetRoomSaleRuleEntity.setRoomTypeId(roomTypeId);
        switchSetRoomSaleRuleEntity.setRoomTypeName(roomName);
        switchSetRoomSaleRuleEntity.setSetStripDailyRule(setRoomSaleRuleVo.isSetCtripDailyRule());
        switchSetRoomSaleRuleEntity.setSaleRuleListJson(JSONObject.toJSONString(saleRuleList));
        Result<Object> result = switchBasicRoomTypeService.setRoomSaleRule(setRoomSaleRuleVo);
        if (result.getCode() == CommonConstants.SUCCESS) {
            switchSetRoomSaleRuleMapper.insert(switchSetRoomSaleRuleEntity);
            log.info("=======设置售卖规则成功====");
        } else {
            log.info("========设置售卖规则失败=====,原因是{}", result.getMsg());
        }
    }

    /**
     * 设置最晚预定时间
     */
    private List<SaleRuleVo> book(List<SaleRuleVo> ctripSale, List<MtHotelGoodsBookRuleDto> bookRuleList, String channel) {
        for (SaleRuleVo saleRuleVo : ctripSale) {
            if (ObjectUtils.isEmpty(bookRuleList) || bookRuleList.size() < 1) {
                saleRuleVo.setSetLastBookTime(false);
            } else {
                for (MtHotelGoodsBookRuleDto mtHotelGoodsBookRuleDto : bookRuleList) {
                    //最晚预定天数(支持设置Ctrip凌晨房，传入-1代表设置凌晨售卖昨日房)
                    //最晚预定小时数(设置Ctrip凌晨房，传入-参数只能为1-7)
                    String mtLatestBookingHours = ObjectUtils.isEmpty(mtHotelGoodsBookRuleDto.getMtLatestBookingHours()) ? "24:00:00" : mtHotelGoodsBookRuleDto.getMtLatestBookingHours();
                    int mtLatestBookingDays = ObjectUtils.isEmpty(mtHotelGoodsBookRuleDto.getMtLatestBookingDays()) ? 0 : mtHotelGoodsBookRuleDto.getMtLatestBookingDays();
                    Long hour = DateUtil.dateDiff(mtLatestBookingHours, "h", "", "");
                    //如果美团返回-1 -1表示支持凌晨预定 携程只有ctrip能设置凌晨房
                    if (channel.equals(SwitchChannelNameEnums.CTRIP.getName())) {
                        saleRuleVo.setSetLastBookTime(true);
                        if (mtHotelGoodsBookRuleDto.getMtLatestBookingDays() == -1) {
                            saleRuleVo.setLastBookTime(String.valueOf((hour > 7 ? 7 : hour)));
                            saleRuleVo.setLastBookDay(mtLatestBookingDays);
                        } else {
                            saleRuleVo.setLastBookDay(0);
                            saleRuleVo.setLastBookTime(String.valueOf(hour));
                        }
                    } else {
                        saleRuleVo.setSetLastBookTime(false);
                        saleRuleVo.setLastBookDay(0);
                        saleRuleVo.setLastBookTime(String.valueOf(hour));
                    }
                    //非Ctrip渠道 设置连住限制
                    if (!channel.equals(SwitchChannelNameEnums.CTRIP.getName())) {
                        //最少连住天数
                        saleRuleVo.setMinLastDay(mtHotelGoodsBookRuleDto.getMtSerialCheckinMin());
                        //最大连住天数
                        saleRuleVo.setMaxLastDay(mtHotelGoodsBookRuleDto.getMtSerialCheckinMax());
                        if (mtHotelGoodsBookRuleDto.getMtSerialCheckinMin() > 0 && mtHotelGoodsBookRuleDto.getMtSerialCheckinMax() > 0) {
                            saleRuleVo.setSetLast(true);
                        }
                        //最少预定间数
                        saleRuleVo.setMinBookRoomNum(mtHotelGoodsBookRuleDto.getMtRoomCountMin());
                        //最大预定间数
                        saleRuleVo.setMaxBookRoomNum(mtHotelGoodsBookRuleDto.getMtRoomCountMax());
                        if (mtHotelGoodsBookRuleDto.getMtRoomCountMin() > 0 && mtHotelGoodsBookRuleDto.getMtRoomCountMax() > 0) {
                            //是否设置间数限制
                            saleRuleVo.setSetBookRoomNum(true);
                        } else {
                            //是否设置间数限制
                            saleRuleVo.setSetBookRoomNum(false);
                        }
                    }
                    saleRuleVo.setSetLastConfirmTime(false);
                }
            }
        }
        return ctripSale;
    }

    /**
     * 设置取消规则,根据美团的产品来设置
     *
     * @param cancelRuleList
     * @param ctrip
     * @param hotelStart
     * @return
     */
    private List<SaleRuleVo> cancel(List<MtHotelGoodsCancelRuleDto> cancelRuleList, String ctrip, int hotelStart) {
        List<SaleRuleVo> saleRuleList = new ArrayList<>();
        //如果美团没有取消规则,默认未不可取消
        if (ObjectUtils.isEmpty(cancelRuleList) || cancelRuleList.size() < 1) {
            SaleRuleVo saleRuleVo = new SaleRuleVo();
            saleRuleVo.setChannelType(ctrip);
            saleRuleVo.setSetCancel(true);
            saleRuleVo.setCanCancel(false);
            saleRuleVo.setDeductType2(1);
            saleRuleList.add(saleRuleVo);
            return saleRuleList;
        }
        //美团的产品规则
        for (MtHotelGoodsCancelRuleDto mtHotelGoodsCancelRuleDto : cancelRuleList) {
            SaleRuleVo saleRuleVo = new SaleRuleVo();
            saleRuleVo.setChannelType(ctrip);
            //0 不可取消 1 可以取消
            if (mtHotelGoodsCancelRuleDto.getMtCancelType() == 0) {
                if (ctrip.equals("ctrip")) {
                    saleRuleVo.setSetCancel(true);
                    saleRuleVo.setCanCancel(false);
                    saleRuleVo.setDeductType2(1);
                } else {
                    saleRuleVo.setSetCancel(true);
                    saleRuleVo.setCanCancel(false);
                    saleRuleVo.setCheckInTime2("0");
                }
                saleRuleList.add(saleRuleVo);
            } else {
                //是否设置取消规则
                saleRuleVo.setSetCancel(true);
                //是否可以取消 true可以 false 不可
                saleRuleVo.setCanCancel(true);
                //可取消提前天数 范围1~22
                int checkInDay = 0;
                checkInDay = (mtHotelGoodsCancelRuleDto.getMtAheadCancelDays() > 0) ? (mtHotelGoodsCancelRuleDto.getMtAheadCancelDays()) : 0;
                saleRuleVo.setCheckInDay(checkInDay);
                //提前取消时间类型：0 时间 1 时间量
                //可取消提前小时数 Ctrip的范围1~23，其他渠道为0~24
                if (mtHotelGoodsCancelRuleDto.getMtDeductType() == 0) {
                    saleRuleVo.setCheckInTime(mtHotelGoodsCancelRuleDto.getMtAheadCancelHours());
                    saleRuleVo.setCheckInDay2(checkInDay);
                    saleRuleVo.setCheckInTime2(DateUtil.getSpecifiedSecondBefore(mtHotelGoodsCancelRuleDto.getMtAheadCancelHours()));
                } else {
                    int hour = Integer.valueOf(mtHotelGoodsCancelRuleDto.getMtAheadCancelHours());
                    String time = (24 - hour) + ":00";
                    saleRuleVo.setCheckInTime(time);
                    saleRuleVo.setCheckInDay2(checkInDay);
                    saleRuleVo.setCheckInTime2(String.valueOf(((24 - hour + 2) > 0) ? (24 - hour + 2) : 0));

                }
                //0-3星级酒店 不扣款  4-5星级酒店 可取消扣百分10% 逾期扣100%
                //可取消扣款类型 Ctrip渠道传0(不扣款)，其他渠道传0，2(扣首晚)，3(按百分比扣款)
                if (hotelStart < 4) {
                    //非携程设置扣款比例,
                    if (!ctrip.equals(SwitchChannelNameEnums.CTRIP.getName())) {
                        saleRuleVo.setDeductType(3);
                        saleRuleVo.setDeductProportion(1);
                        saleRuleVo.setDeductType2(3);
                        saleRuleVo.setDeductProportion2(99);
                    } else { //携程设置不扣款,逾期扣全款
                        saleRuleVo.setDeductType(0);
                        saleRuleVo.setDeductType2(1);
                    }
                } else {
                    if (!ctrip.equals(SwitchChannelNameEnums.CTRIP.getName())) {
                        saleRuleVo.setDeductType(3);
                        //可取消扣款比例  扣10 逾期扣百分比
                        saleRuleVo.setDeductProportion(10);
                        saleRuleVo.setDeductType2(3);
                        saleRuleVo.setDeductProportion2(99);
                    } else { //携程设置不扣款,逾期扣全款
                        saleRuleVo.setDeductType(0);
                        saleRuleVo.setDeductType2(1);
                    }
                }
                saleRuleVo.setSetAutoChange(false);
                saleRuleList.add(saleRuleVo);
            }
        }
        return saleRuleList;
    }

    /**
     * 设置售卖房型上下线
     * hotelId 子酒店id
     * roomTypeId 售卖房型id
     * 状态：0是下线，1是上线，2是无效
     */
    public void setRoomOnlineOffline(int hotelId, int roomTypeId, int roomStatus) {
        log.info("============开始设置售卖房型上下线================");
        //再去设置售卖房型上下线
        SetRoomOnlineOfflineVo setRoomOnlineOfflineVo = new SetRoomOnlineOfflineVo();
        setRoomOnlineOfflineVo.setChannel("Ctrip,Qunar");
        setRoomOnlineOfflineVo.setHotelId(hotelId);
        setRoomOnlineOfflineVo.setRoomStatus(roomStatus);
        setRoomOnlineOfflineVo.setRoomTypeId(roomTypeId);
        Result<Object> result = switchBasicRoomTypeService.setRoomOnlineOffline(setRoomOnlineOfflineVo);
        if (result.getCode() == CommonConstants.SUCCESS) {
            log.info("=================设置售卖房型上下线成功,{}======================", result.getMsg());
        } else {
            log.info("==================设置售卖房型上下先失败{}=============", result.getMsg());
        }
    }

    /**
     * 设置房型库存房态共享
     * hotelId 子酒店id
     * roomTypeId 售卖房型id
     * shareType 共享库存与房态 RoomInventory:库存，RoomStatus:房态
     * "isShare": false, //是否共享
     * "baseChannel": "String"//共享里，库存，房态的基准渠道。Ctrip,Qunar ,ChannelA,B2B
     */
    public void setRoomShare(int hotelId, int roomTypeId, String channel) {
        log.info("======================设置房型库存房态共享=====================");
        SetRoomShareVo setRoomShareVo = new SetRoomShareVo();
        setRoomShareVo.setHotelId(hotelId);
        setRoomShareVo.setRoomId(roomTypeId);
        setRoomShareVo.setShare(true);
        setRoomShareVo.setShareType("RoomInventory");
        setRoomShareVo.setBaseChannel(channel);
        Result<Object> result = switchBasicRoomTypeService.setRoomShare(setRoomShareVo);
        if (result.getCode() == CommonConstants.SUCCESS) {
            setRoomShareVo.setShareType("RoomStatus");
            log.info("=============设置库存共享成功=================,返回信息{}", result.getMsg());
            Result<Object> result1 = switchBasicRoomTypeService.setRoomShare(setRoomShareVo);
            if (result1.getCode() == CommonConstants.SUCCESS) {
                log.info("=============设置房态共享成功=================,返回信息{}", result.getMsg());
            }
        }
    }

    /**
     * 获取早餐
     */
    public Map<String, Integer> getBreakFastId(List<MtHotelGoodsBreakfastDto> mtHotelGoodsBreakfastDtos) {
      /*  String[] names = new String[]{"不定", "无早", "单早", "含早", "双早", "早+晚餐", "早+午餐", "早+午+晚餐", "晚餐" + "午餐" + "午+晚餐"};*/
        int[] breakIds = new int[]{6, 498160, 7, 8, 640292, 111, 460276, 112, 1211906, 5454133, 5454137};
        Map<String, Integer> map = new HashMap<>();
        //默认规则算出的早餐id
        int defaultBreakId = 498160;
        //餐食份数
        int mealCount = 0;
        //多个早餐规则时需根据规则开始结束日期判断满足哪个规则，默认无早
        if (ObjectUtils.isEmpty(mtHotelGoodsBreakfastDtos) || mtHotelGoodsBreakfastDtos.size() < 1) {
            map.put("mealCount", mealCount);
            map.put("breakId", breakIds[1]);
            return map;
        }
        for (MtHotelGoodsBreakfastDto mtHotelGoodsBreakfastDto : mtHotelGoodsBreakfastDtos) {
            //1.有默认生效规则
            if (mtHotelGoodsBreakfastDto.getMtStartDate().equals("0")) {
                int type = mtHotelGoodsBreakfastDto.getMtBreakfastType();
                if (type == 0) {
                    defaultBreakId = breakIds[1];
                } else if (type == 1) {
                    //根据breakfastNum来设置
                    defaultBreakId = getBreakFastByNum(mtHotelGoodsBreakfastDto.getMtBreakfastNum());
                    mealCount = mtHotelGoodsBreakfastDto.getMtBreakfastNum();
                } else if (type == 2) {//付费早餐,不定
                    defaultBreakId = breakIds[1];
                }
            }
        }
        map.put("breakId", defaultBreakId);
        map.put("mealCount", mealCount);
        return map;
    }

    /**
     * 获取早餐,根据每日查询出来的时间
     * @param breakFast
     */
    public Map<String, Integer> getBreakFastIdByUpdate(List<MtHotelGoodsBreakfastVo> breakFast) {
        /*  String[] names = new String[]{"不定", "无早", "单早", "含早", "双早", "早+晚餐", "早+午餐", "早+午+晚餐", "晚餐" + "午餐" + "午+晚餐"};*/
        int[] breakIds = new int[]{6, 498160, 7, 8, 640292, 111, 460276, 112, 1211906, 5454133, 5454137};
        Map<String, Integer> map = new HashMap<>();
        //当前的时间
        String current_time = DateUtil.getToday2();
        //时间段找出的日期
        int breakId = 498160;
        //餐食份数
        int mealCount = 1;
        //多个早餐规则时需根据规则开始结束日期判断满足哪个规则，默认无早
        for (MtHotelGoodsBreakfastVo MtHotelGoodsBreakfastVo : breakFast) {
            //1.有默认生效规则
            if (MtHotelGoodsBreakfastVo.getInStartDate()>0) {
                if(!ObjectUtils.isEmpty(MtHotelGoodsBreakfastVo.getInStartDate())){
                    if (DateUtil.commpareDay(String.valueOf(MtHotelGoodsBreakfastVo.getInStartDate()), current_time)) {
                        int type = MtHotelGoodsBreakfastVo.getBreakfastType();
                        if (type == 0) {
                            breakId =breakIds[1];
                        } else if (type == 1) {
                            //根据breakfastNum来设置
                            breakId = getBreakFastByNum(MtHotelGoodsBreakfastVo.getBreakfastNum());
                            if (breakId == 8) {
                                mealCount = MtHotelGoodsBreakfastVo.getBreakfastNum();
                            }
                        } else if (type == 2) {//付费早餐,不定
                            breakId = breakIds[6];
                        }
                    }
                }
            }
        }
        map.put("mealCount", mealCount);
        map.put("breakId", breakId);
        return map;
    }

    /**
     * 根据免费早餐数来设置
     */
    public int getBreakFastByNum(int breakfastNum) {
        int[] breakIds = new int[]{6, 498160, 7, 8, 640292, 111, 460276, 112, 1211906, 5454133, 5454137};
        int breakId=breakIds[1];
        if (breakfastNum == 1) {
            breakId= breakIds[2];
        } else if (breakfastNum == 2) {
            breakId=breakIds[4];
        } else if(breakfastNum == 3){
            breakId=breakIds[3];
        }else{
            breakId=breakIds[3];
        }
        return breakId;
    }

    /**
     * 获取创建售卖房型信息
     *
     * @param roomId 售卖房型id
     * @return
     */
    @Override
    public SwitchCreateRoomDto selectOneDtoByRoomId(Integer roomId) {
        Example example = new Example(SwitchCreateRoomEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("mtRoomId", roomId);
        SwitchCreateRoomEntity entity = switchCreateRoomMapper.selectOneByExample(example);
        if (null != entity) {
            SwitchCreateRoomDto dto = new SwitchCreateRoomDto();
            dto.setHotelId(entity.getHotelId());
            dto.setBasicRoomTypeId(entity.getBasicRoomTypeId());
            dto.setRoomTypeId(0);
            dto.setRelationRoomId(0);
            dto.setRateCodePropertyValueIdList(entity.getRateCodePropertyValueIdList());
            dto.setMealCount(entity.getMealCount());
            dto.setApplicabilityInfo(entity.getApplicabilityInfo());
            dto.setApplicabilityList(entity.getApplicabilityList());
            dto.setPriceSystemName(entity.getPriceSystemName());
            dto.setBlackChannelList(entity.getBlackChannelList());
            dto.setMtHotelId(entity.getMtHotelId());
            dto.setMtGoodId(entity.getMtGoodId());
            dto.setMtRoomId(entity.getMtRoomId());
            dto.setMtGoodName(entity.getMtGoodName());
            return dto;
        }
        return null;
    }

    /**
     * 分页获取，返回携程创建售卖房型对应的美团产品ids
     *
     * @param index
     * @param pageSize
     * @return
     */
    @Override
    public Result<Object> getMtIdsPageAboutSwitchSaleRoom(Integer index, Integer pageSize) {
        Map<String, Integer> map = new HashMap();
        map.put("positionIndex", index);
        map.put("nextPageSize", pageSize);
        List<SwitchSaleRoomAboutIdsDto> list = switchCreateRoomMapper.getMtIdsPageAboutSwitchSaleRoom(map);
        return new Result<>(list);

    }

    /**
     * 根据售卖房型id 获取美团产品id
     */
    @Override
    public int getMtGoodIdByRoomId(int roomId) {
        return switchCreateRoomMapper.getMtGoodsId(roomId);
    }

    @Override
    public SwitchCreateRoomDto getMtInfoBySwitchRoomId(int roomId) {
        return switchCreateRoomMapper.getMtInfoBySwitchRoomId(roomId);
    }

    @Override
    public SwitchCreateRoomDto selectOneDtoByMtGoodsId(Integer mtGoodsId, String mtHotelId) {
        Example example = new Example(SwitchCreateRoomEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("mtGoodId", mtGoodsId);
        criteria.andEqualTo("mtHotelId", mtHotelId);
        SwitchCreateRoomEntity entity = switchCreateRoomMapper.selectOneByExample(example);
        if (null != entity) {
            SwitchCreateRoomDto dto = new SwitchCreateRoomDto();
            dto.setHotelId(entity.getHotelId());
            dto.setBasicRoomTypeId(entity.getBasicRoomTypeId());
            dto.setRoomTypeId(0);
            dto.setRelationRoomId(0);
            dto.setRateCodePropertyValueIdList(entity.getRateCodePropertyValueIdList());
            dto.setMealCount(entity.getMealCount());
            dto.setApplicabilityInfo(entity.getApplicabilityInfo());
            dto.setApplicabilityList(entity.getApplicabilityList());
            dto.setPriceSystemName(entity.getPriceSystemName());
            dto.setBlackChannelList(entity.getBlackChannelList());
            dto.setMtHotelId(entity.getMtHotelId());
            dto.setMtGoodId(entity.getMtGoodId());
            dto.setMtRoomId(entity.getMtRoomId());
            dto.setMtGoodName(entity.getMtGoodName());
            return dto;
        }
        return null;
    }

    @Override
    public List<SwitchSaleRoomAboutIdsDto> selectDtosByMtHotelId(String mtHotelId) {
        Example example = new Example(SwitchCreateRoomEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("mtHotelId", mtHotelId);
        criteria.andEqualTo("state", 0);
        criteria.andEqualTo("up", 0);
        List<SwitchSaleRoomAboutIdsDto> dtoList = new ArrayList<>();
        List<SwitchCreateRoomEntity> list = switchCreateRoomMapper.selectByExample(example);
        if (null != list && list.size() > 0) {
            for (SwitchCreateRoomEntity entity : list) {
                SwitchSaleRoomAboutIdsDto dto = new SwitchSaleRoomAboutIdsDto();
                dto.setHotelId(String.valueOf(entity.getHotelId()));
                dto.setMtRoomId(entity.getMtRoomId());
                dto.setMtGoodId(entity.getMtGoodId());
                dto.setSaleRoomId(String.valueOf(entity.getRoomId()));
                dtoList.add(dto);
            }
            return dtoList;
        }
        return null;
    }

    /**
     * @author: lmf
     * @date: 2020/8/21 17:29
     * 根据美团的酒店ID 美团的产品获取信息内容
     */
    @Override
    public SwitchCreateRoomEntity selectMtHotelAndMtGoodId(int mtHotelId, int mtGoodId) {
        return switchCreateRoomMapper.selectByMtGoodsIdAndHotelId(mtHotelId, mtGoodId);
    }

    /**
     * <H1>根据酒店ID获取酒店相关信息</H1>
     *
     * @param hotelIdArr 实体对象
     * @return java.util.List<hotel.switchs.oversea.api.VO.HotelRoomInfoVO>
     * @author HuangT
     * @Date 2020/9/10 17:26
     */
    @Override
    public List<SwitchHotelActiveTag> getHotelRoomInfo(int[] hotelIdArr) {
        return switchCreateRoomMapper.getHotelRoomInfo(hotelIdArr);
    }

    /**
     * @author: lmf
     * @date: 2020/9/14 13:54
     * 重新推送售卖规则
     */
    @Override
    public Result<Object> setCancelRule() {
        //1.获取所有参与重新设置的取消房型id
        List<SwitchCancelRuleSetEntity> switchCancelRuleSetEntities = switchCancelRuleSetMapper.selectAll();
        for (SwitchCancelRuleSetEntity switchCancelRuleSetEntity : switchCancelRuleSetEntities) {
            if (switchCancelRuleSetEntity.getRoomId() < 1) {
                List<SwitchCreateRoomEntity> switchCreateRoomEntities = switchCreateRoomMapper.selectByHotelId(switchCancelRuleSetEntity.getHotelId());
                for (SwitchCreateRoomEntity switchCreateRoomEntity : switchCreateRoomEntities) {
                    Map<String, Object> param = new HashMap<>();
                    param.put("hotelId", switchCreateRoomEntity.getMtHotelId());
                    param.put("goodsType", 1);
                    param.put("goodsId", switchCreateRoomEntity.getMtGoodId());
                    Date date = new Date();
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                    param.put("checkinDate", formatter.format(date));
                    param.put("checkoutDate", DateUtil.getNextDay(formatter.format(date)));
                    Result<List<MtHotelGoodsCancelRuleVo>> result = switchRestTempaleUtilsService.sendPostRequestGetCancelByMtHotelIdAndGoodId(CommonUrlConstants.MEITUAN_HOST + "/" + MtRequestUrlEnums.GET_CANCEL_RULE_BY_MT_INFO.getUrl(), param);
                    if(ObjectUtils.isEmpty(result.getData())){
                        continue;
                    }
                    List<MtHotelGoodsCancelRuleVo> cancelRuleList = result.getData();
                    setRoomSaleRuleAgein(switchCreateRoomEntity.getHotelId(), switchCreateRoomEntity.getRoomId(), switchCreateRoomEntity.getRoomName(), cancelRuleList, switchCancelRuleSetEntity.getTime());
                    try {
                        Thread.sleep(600);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            } else {
                SwitchCreateRoomEntity switchCreateRoomEntity = switchCreateRoomMapper.selectByHotelIdAndRoomId(switchCancelRuleSetEntity.getHotelId(), switchCancelRuleSetEntity.getRoomId());
                Map<String, Object> param = new HashMap<>();
                param.put("hotelId", switchCreateRoomEntity.getMtHotelId());
                param.put("goodsType", 1);
                param.put("goodsId", switchCreateRoomEntity.getMtGoodId());
                Date date = new Date();
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                param.put("checkinDate", formatter.format(date));
                param.put("checkoutDate", DateUtil.getNextDay(formatter.format(date)));
                Result<List<MtHotelGoodsCancelRuleVo>> result = switchRestTempaleUtilsService.sendPostRequestGetCancelByMtHotelIdAndGoodId(CommonUrlConstants.MEITUAN_HOST + "/" + MtRequestUrlEnums.GET_CANCEL_RULE_BY_MT_INFO.getUrl(), param);
                List<MtHotelGoodsCancelRuleVo> cancelRuleList = result.getData();
                setRoomSaleRuleAgein(switchCreateRoomEntity.getHotelId(), switchCreateRoomEntity.getRoomId(), switchCreateRoomEntity.getRoomName(), cancelRuleList, switchCancelRuleSetEntity.getTime());
            }
        }
        return null;
    }


    /**
     * 设置售卖规则: 设置房型的取消规则 默认比美团提前俩个小时
     * 比如美团是入住前18:00可取消 我们设置就往前面推移2个小时(暂时取消)
     *
     * @param hotelId        子酒店id
     * @param roomTypeId     售卖房型id
     * @param roomName       售卖房型名称
     * @param cancelRuleList 美团产品的取消规则
     * @param
     */
    private void setRoomSaleRuleAgein(Integer hotelId, int roomTypeId, String roomName, List<MtHotelGoodsCancelRuleVo> cancelRuleList, String time) {
        //封装设置售卖规则表单结构
        SetRoomSaleRuleVo setRoomSaleRuleVo = new SetRoomSaleRuleVo();
        setRoomSaleRuleVo.setHotelId(hotelId);
        //获取酒店的星级
        Integer hotelStart = switchHotelInfoMapper.selectHotelStarByHotelId(hotelId);
        if (ObjectUtils.isEmpty(hotelStart)) {
            hotelStart = 1;
        }
        RoomTypeVo roomTypeVo = new RoomTypeVo();
        roomTypeVo.setRoomTypeId(roomTypeId);
        roomTypeVo.setRoomTypeName(roomName);
        //设置售卖房型列表
        List<RoomTypeVo> roomTypeList = new ArrayList<>();
        roomTypeList.add(roomTypeVo);
        setRoomSaleRuleVo.setRoomTypeList(roomTypeList);
        setRoomSaleRuleVo.setSetCtripDailyRule(false);
        //设置规则
        List<SaleRuleVo> saleRuleList = new ArrayList<>();
        //1.ctrip 设置取消规则
        List<SaleRuleVo> ctripSale = cancelByTime(cancelRuleList, SwitchChannelNameEnums.CTRIP.getName(), hotelStart, time);
        //1.ctrip 设置预定规则
        ctripSale = book(ctripSale, null, SwitchChannelNameEnums.CTRIP.getName());
        //去哪儿 设置取消规则
        List<SaleRuleVo> otherSale = cancelByTime(cancelRuleList, SwitchChannelNameEnums.QUNAR.getName(), hotelStart, time);
        //去哪儿  设置预定时间
        otherSale = book(otherSale, null, SwitchChannelNameEnums.QUNAR.getName());
        //b2b设置取消规则
        List<SaleRuleVo> b2bSale = cancelByTime(cancelRuleList, SwitchChannelNameEnums.B2B.getName(), hotelStart, time);
        //b2b设置预定规则
        b2bSale = book(b2bSale, null, SwitchChannelNameEnums.B2B.getName());
        /*//channel设置取消规则
        List<SaleRuleVo> channelSale = cancelByTime(cancelRuleList, SwitchChannelNameEnums.CHANNEL_A.getName(), hotelStart, time);
        //channel设置预定规则
        channelSale = book(channelSale, null, SwitchChannelNameEnums.CHANNEL_A.getName());*/
        for (SaleRuleVo saleRuleVo : ctripSale) {
            //携程是否设置最晚确认时间 默认0 18
           /* saleRuleVo.setSetLastConfirmTime(true);
            saleRuleVo.setLastBookDay(0);
            saleRuleVo.setLastConfirmTime(String.valueOf(15));*/
            saleRuleVo.setSetLimitVisiable(false);
            saleRuleList.add(saleRuleVo);
        }
        for (SaleRuleVo saleRuleVo : otherSale) {
            saleRuleVo.setSetLimitVisiable(false);
            saleRuleList.add(saleRuleVo);
        }
        for (SaleRuleVo saleRuleVo : b2bSale) {
            saleRuleVo.setSetLimitVisiable(false);
            saleRuleList.add(saleRuleVo);
        }
    /*    for (SaleRuleVo saleRuleVo : channelSale) {
            saleRuleVo.setSetLimitVisiable(false);
            saleRuleList.add(saleRuleVo);
        }*/
        log.info("=============该产品设置的规则长度有{}==========", saleRuleList.size());
        setRoomSaleRuleVo.setSaleRuleList(saleRuleList);
        log.info("===============更新售卖规则的请求数据为{}===========", setRoomSaleRuleVo);
        //把规则记录到表里
        SwitchSetRoomSaleRuleEntity switchSetRoomSaleRuleEntity = new SwitchSetRoomSaleRuleEntity();
        switchSetRoomSaleRuleEntity.setId(StringUtil.getUUID());
        switchSetRoomSaleRuleEntity.setCreateTime(new Date());
        switchSetRoomSaleRuleEntity.setHotelId(hotelId);
        switchSetRoomSaleRuleEntity.setRoomTypeId(roomTypeId);
        switchSetRoomSaleRuleEntity.setRoomTypeName(roomName);
        switchSetRoomSaleRuleEntity.setSetStripDailyRule(setRoomSaleRuleVo.isSetCtripDailyRule());
        switchSetRoomSaleRuleEntity.setSaleRuleListJson(JSONObject.toJSONString(saleRuleList));
        Result<Object> result = switchBasicRoomTypeService.setRoomSaleRule(setRoomSaleRuleVo);
        if (result.getCode() == CommonConstants.SUCCESS) {
            switchSetRoomSaleRuleMapper.insert(switchSetRoomSaleRuleEntity);
            log.info("=======设置售卖规则成功====");
        } else {
            log.info("========设置售卖规则失败=====,原因是{}", result.getMsg());
        }
    }

    /**
     * 设置取消规则,根据美团的产品来设置
     * 如果美团可以取消，自行设置取消时间
     *
     * @param cancelRuleList
     * @param ctrip
     * @param hotelStart
     * @return
     */
    private List<SaleRuleVo> cancelByTime(List<MtHotelGoodsCancelRuleVo> cancelRuleList, String ctrip, int hotelStart, String entTime) {
        List<SaleRuleVo> saleRuleList = new ArrayList<>();
        //如果美团没有取消规则,默认未不可取消
        if (ObjectUtils.isEmpty(cancelRuleList) || cancelRuleList.size() < 1) {
            SaleRuleVo saleRuleVo = new SaleRuleVo();
            saleRuleVo.setChannelType(ctrip);
            saleRuleVo.setSetCancel(true);
            saleRuleVo.setCanCancel(false);
            saleRuleVo.setDeductType2(1);
            saleRuleList.add(saleRuleVo);
            return saleRuleList;
        }
        //美团的产品规则
        for (MtHotelGoodsCancelRuleVo mtHotelGoodsCancelRuleDto : cancelRuleList) {
            SaleRuleVo saleRuleVo = new SaleRuleVo();
            saleRuleVo.setChannelType(ctrip);
            //0 不可取消 1 可以取消
            if (mtHotelGoodsCancelRuleDto.getCancelType() == 0) {
                if (ctrip.equals("ctrip")) {
                    saleRuleVo.setSetCancel(true);
                    saleRuleVo.setCanCancel(false);
                    saleRuleVo.setDeductType2(1);
                } else {
                    saleRuleVo.setSetCancel(true);
                    saleRuleVo.setCanCancel(false);
                    saleRuleVo.setCheckInTime2("0");
                }
                saleRuleList.add(saleRuleVo);
            } else {
                //是否设置取消规则
                saleRuleVo.setSetCancel(true);
                //是否可以取消 true可以 false 不可
                saleRuleVo.setCanCancel(true);
                //可取消提前天数 范围1~22
                int checkInDay = 0;
                checkInDay = (mtHotelGoodsCancelRuleDto.getAheadCancelDays() > 0) ? (mtHotelGoodsCancelRuleDto.getAheadCancelDays()) : 0;
                saleRuleVo.setCheckInDay(checkInDay);
                saleRuleVo.setDeductProportion(1);
                //提前取消时间类型：0 时间 1 时间量
                //可取消提前小时数 Ctrip的范围1~23，其他渠道为0~24
                if (mtHotelGoodsCancelRuleDto.getDeductType() == 0) {
                    saleRuleVo.setCheckInTime(entTime + ":00");
                    saleRuleVo.setCheckInDay2((checkInDay - 1) > 0 ? (checkInDay - 1) : 0);
                    saleRuleVo.setCheckInTime2(entTime + ":00");
                } else {
                    String time = entTime + ":00";
                    saleRuleVo.setCheckInTime(time);
                    saleRuleVo.setCheckInDay2((checkInDay - 1) > 0 ? (checkInDay - 1) : 0);
                    saleRuleVo.setCheckInTime2((Integer.valueOf(entTime) + 2) + ":00");

                }
                //0-3星级酒店 不扣款  4-5星级酒店 可取消扣百分10% 逾期扣100%
                //可取消扣款类型 Ctrip渠道传0(不扣款)，其他渠道传0，2(扣首晚)，3(按百分比扣款)
                if (hotelStart < 4) {
                    //非携程设置扣款比例,
                    if (!ctrip.equals(SwitchChannelNameEnums.CTRIP.getName())) {
                        saleRuleVo.setDeductType(3);
                        saleRuleVo.setDeductProportion(1);
                        saleRuleVo.setDeductType2(3);
                        saleRuleVo.setDeductProportion2(99);
                    } else { //携程设置不扣款,逾期扣全款
                        saleRuleVo.setDeductType(0);
                        saleRuleVo.setDeductType2(1);
                    }
                } else {
                    if (!ctrip.equals(SwitchChannelNameEnums.CTRIP.getName())) {
                        saleRuleVo.setDeductType(3);
                        //可取消扣款比例  扣10 逾期扣百分比
                        saleRuleVo.setDeductProportion(10);
                        saleRuleVo.setDeductType2(3);
                        saleRuleVo.setDeductProportion2(99);
                    } else { //携程设置不扣款,逾期扣全款
                        saleRuleVo.setDeductType(0);
                        saleRuleVo.setDeductType2(1);
                    }
                }
                saleRuleVo.setSetAutoChange(false);
                saleRuleList.add(saleRuleVo);
            }
        }
        return saleRuleList;
    }

    /**
     * @author: lmf
     * @date: 2020/9/15 11:37
     * 获取房型列表
     */
    @Override
    public Result<Object> getRoomList(GetRoomListVo getRoomListVo) {
        PageHelper.startPage(getRoomListVo.getPageCode(), getRoomListVo.getPageSize());
        List<SwitchCreateRoomInfo> list = switchCreateRoomMapper.selectRoomList(getRoomListVo);
        PageInfo<SwitchCreateRoomInfo> pageInfo = new PageInfo<>(list);
        return new Result<>(pageInfo);
    }
    /**
     * @author: lmf
     * @date: 2020/9/18 14:05
     * 设置售卖房型失效
     */
    @Override
    public Result<Object> loseRoom() {
        //1.获取有问题的售卖房型信息
       //List<SwitchCreateRoomEntity>  switchCreateRoomEntities=switchCreateRoomMapper.selectMatchError();
       List<SwitchCreateRoomEntity>  switchCreateRoomEntities=switchCreateRoomMapper.selectAll();
       log.info("=========创建错误的售卖房型长度为{}==============",switchCreateRoomEntities.size());
       for(SwitchCreateRoomEntity switchCreateRoomEntity:switchCreateRoomEntities){
           modifyRoomStatus(switchCreateRoomEntity.getRoomId(),switchCreateRoomEntity.getHotelId(),0);
           try {
               Thread.sleep(600);
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
       }
       return new Result<>();
    }

    /**
     * 下架售卖房型
     * @param roomId 售卖房型id
     * @param hotelId 子酒店id
     */
    public void modifyRoomStatus(int roomId,int hotelId,int status){
        //没有库存,下架该售卖房型
        SetRoomOnlineOfflineVo setRoomOnlineOfflineVo=new SetRoomOnlineOfflineVo();
        setRoomOnlineOfflineVo.setRoomTypeId(roomId);
        setRoomOnlineOfflineVo.setHotelId(hotelId);
        setRoomOnlineOfflineVo.setRoomStatus(status);
        setRoomOnlineOfflineVo.setChannel(StringUtil.firstToUpperCase(SwitchChannelNameEnums.CTRIP.getName()));
        Result<Object> result=switchBasicRoomTypeService.setRoomOnlineOffline(setRoomOnlineOfflineVo);
        try {
            Thread.sleep(6000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        /*if(result.getCode()==CommonConstants.SUCCESS){
            setRoomOnlineOfflineVo.setChannel(StringUtil.firstToUpperCase(SwitchChannelNameEnums.QUNAR.getName()));
            Result<Object> result1=switchBasicRoomTypeService.setRoomOnlineOffline(setRoomOnlineOfflineVo);
            if(result1.getCode()==CommonConstants.SUCCESS){
                setRoomOnlineOfflineVo.setChannel("B2B");
                try {
                    Thread.sleep(6000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                switchBasicRoomTypeService.setRoomOnlineOffline(setRoomOnlineOfflineVo);
            }
        }*/
    }

    /**
     *
     * @author: lmf
     * @time: 2020/10/28 16:48
     * 更新售卖房型餐食信息
     * @param pushHotelBreakfastVo
     */
    @Override
    public Result<Object> updateCreateRoomBreak(PushHotelBreakfastVo pushHotelBreakfastVo) {
        int mtHotelId=pushHotelBreakfastVo.getMtHotelId();
        List<PushHotelBreakfastDetailVo> pushHotelBreakfastDetailVos=pushHotelBreakfastVo.getGoodsBreakfast();
        String mtGoodId;
        tab:for(PushHotelBreakfastDetailVo pushHotelBreakfastDetailVo:pushHotelBreakfastDetailVos){
            mtGoodId=pushHotelBreakfastDetailVo.getGoodsId();
            if(ObjectUtils.isEmpty(pushHotelBreakfastDetailVo.getBreakFast())||pushHotelBreakfastDetailVo.getBreakFast().size()<1){
                continue tab;
            }
            Map<String, Integer> map = getBreakFastIdByUpdate(pushHotelBreakfastDetailVo.getBreakFast());
            SwitchCreateRoomEntity switchCreateRoomEntity=switchCreateRoomMapper.selectByMtGoodsIdAndHotelId(Integer.valueOf(mtGoodId),mtHotelId);
            UpdateRateCodeVo updateRateCodeVo=new UpdateRateCodeVo();
            updateRateCodeVo.setHotelId(switchCreateRoomEntity.getHotelId());
            updateRateCodeVo.setRoomTypeId(switchCreateRoomEntity.getRoomId());
            updateRateCodeVo.setRoomName(switchCreateRoomEntity.getRoomName());
            updateRateCodeVo.setSetBreakfastRate(true);
            int breakFast = map.get("breakId");
            if (breakFast == 8) {
                breakFast=(breakFast < 1) ? SwitchBreakFastEnums.NO.getCode() : breakFast;
                updateRateCodeVo.setBreakfastRateProperty(String.valueOf(breakFast));
                updateRateCodeVo.setMealCount(map.get("mealCount"));
            } else {
                breakFast=(breakFast < 1) ? SwitchBreakFastEnums.NO.getCode() : breakFast;
                updateRateCodeVo.setBreakfastRateProperty(String.valueOf(breakFast));
            }
            updateRateCodeVo.setSetBreakfastDefault(false);
            updateRateCodeVo.setSetBreakfastRate(true);
            log.info("========请求的数据为{}====",JacksonUtil.objectToJson(updateRateCodeVo));
            Result<Object> result=switchBasicRoomTypeService.updateRateCode(updateRateCodeVo);
            log.info("============更新餐食成功===============,返回信息为{}",result.getMsg());
            try {
                Thread.sleep(6000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return new Result<>();
    }
    /**
     *
     * @author: lmf
     * @time: 2020/10/28 17:24
     * 删除售卖房型记录 新增售卖房型失效记录 并清除缓存
     */
    public void saveCreateRoomOpear(SwitchCreateRoomEntity switchCreateRoomEntity){
        SetRoomOnlineOfflineVo setRoomOnlineOfflineVo=new SetRoomOnlineOfflineVo();
        setRoomOnlineOfflineVo.setRoomTypeId(switchCreateRoomEntity.getRoomId());
        setRoomOnlineOfflineVo.setHotelId(switchCreateRoomEntity.getHotelId());
        setRoomOnlineOfflineVo.setChannel("Ctrip");
        setRoomOnlineOfflineVo.setRoomStatus(2);
        switchBasicRoomTypeService.setRoomOnlineOffline(setRoomOnlineOfflineVo);
        SwitchCreateRoomFailureEntity switchCreateRoomFailureEntity=new SwitchCreateRoomFailureEntity();
        BeanUtils.copyProperties(switchCreateRoomEntity,switchCreateRoomFailureEntity);
        switchCreateRoomFailureEntity.setId(StringUtil.getUUID());
        int flag=switchCreateRoomFailureMapper.insert(switchCreateRoomFailureEntity);
        if(flag>0){
            switchCreateRoomMapper.deleteRoomInfoByMtHotelIdAndMtHotelRoomIdAndMtGoodId(switchCreateRoomEntity.getMtHotelId(), Integer.valueOf(switchCreateRoomEntity.getMtRoomId()),switchCreateRoomEntity.getMtGoodId());
            //4.从缓存中清除
            String key = CommonConstants.SWITCH_SELLING_THE_HOUSE_TYPE + switchCreateRoomEntity.getMtHotelId() + "_" + switchCreateRoomEntity.getMtGoodId();
            redisService.remove(key);
        }
    }


    /**
     * 产品失效问题
     * @param productFailureVo
     * @return
     */
    @Override
    @Transactional
    public Result<Object> productFailure(ProductFailureVo productFailureVo) {
        ExecutorService executorService = Executors.newFixedThreadPool(1);
        executorService.submit(new ProductThread(productFailureVo));
        executorService.shutdown();
        return new Result<>();
    }

    /**
     * 产品失效线程
     */
    class ProductThread implements Runnable{
        private ProductFailureVo productFailureVo;

        public ProductThread(ProductFailureVo productFailureVo) {
            this.productFailureVo = productFailureVo;
        }

        @Override
        public void run() {
            log.info("===========产品失效的数据为{}===",JacksonUtil.objectToJson(this.productFailureVo));
            for(ProductFailureDetailVo productFailureDetailVo:this.productFailureVo.getGoods()){
                SwitchReceiveGoodsLogEntity switchReceiveGoodsLogEntity=new SwitchReceiveGoodsLogEntity();
                try {
                    Thread.sleep(20);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (this){
                    switchReceiveGoodsLogEntity.setId(StringUtil.getUUID());
                }
                switchReceiveGoodsLogEntity.setCreateTime(new Date());
                switchReceiveGoodsLogEntity.setGenre(productFailureDetailVo.getGenre());
                switchReceiveGoodsLogEntity.setModifyTime(new Date());
                switchReceiveGoodsLogEntity.setMtGoodId(productFailureDetailVo.getMtGoodId());
                switchReceiveGoodsLogEntity.setMtHotelId(this.productFailureVo.getMtHotelId());
                switchReceiveGoodsLogEntity.setMtRoomId(productFailureDetailVo.getMtRoomId());
                switchReceiveGoodsLogEntity.setRemark("无");
                log.info("=======switchReceiveGoodsLogEntity的值为{}========",switchReceiveGoodsLogEntity);
                switchReceiveGoodsLogMapper.insert(switchReceiveGoodsLogEntity);
                //0 代表失效
                if(productFailureDetailVo.getGenre()==0){
                    //1.推送携程接口，设置售卖无效
                    SwitchCreateRoomEntity switchCreateRoomEntity=switchCreateRoomMapper.selectRoomInfoByMtHotelIdAndMtHotelRoomIdAndMtGoodId(this.productFailureVo.getMtHotelId(),productFailureDetailVo.getMtRoomId(),productFailureDetailVo.getMtGoodId());
                    if(!ObjectUtils.isEmpty(switchCreateRoomEntity)){
                        SetRoomOnlineOfflineVo setRoomOnlineOfflineVo=new SetRoomOnlineOfflineVo();
                        setRoomOnlineOfflineVo.setRoomTypeId(switchCreateRoomEntity.getRoomId());
                        setRoomOnlineOfflineVo.setHotelId(switchCreateRoomEntity.getHotelId());
                        setRoomOnlineOfflineVo.setChannel("Ctrip");
                        setRoomOnlineOfflineVo.setRoomStatus(2);
                        Result<Object> result=switchBasicRoomTypeService.setRoomOnlineOffline(setRoomOnlineOfflineVo);
                      //  setRoomOnlineOfflineVo.setChannel("Qunar");
                       // switchBasicRoomTypeService.setRoomOnlineOffline(setRoomOnlineOfflineVo);
                        //设置无效成功
                        if(result.getCode()==CommonConstants.SUCCESS){
                            //2.添加到产品失效库
                            SwitchCreateRoomFailureEntity switchCreateRoomFailureEntity=new SwitchCreateRoomFailureEntity();
                            BeanUtils.copyProperties(switchCreateRoomEntity,switchCreateRoomFailureEntity);
                            switchCreateRoomFailureEntity.setId(switchCreateRoomFailureEntity.getId()+StringUtil.getFourRandom());
                            switchCreateRoomFailureEntity.setModifyTime(new Date());
                            int flag=switchCreateRoomFailureMapper.insert(switchCreateRoomFailureEntity);
                            //3.从产品库删除
                            if(flag>0){
                                switchCreateRoomMapper.deleteRoomInfoByMtHotelIdAndMtHotelRoomIdAndMtGoodId(productFailureVo.getMtHotelId(),productFailureDetailVo.getMtRoomId(),productFailureDetailVo.getMtGoodId());
                                //清除推送记录
                                switchPushRoomDataMapper.deleteByRoomId(switchCreateRoomEntity.getRoomId());
                                //4.从缓存中清除
                                String key = CommonConstants.SWITCH_SELLING_THE_HOUSE_TYPE + switchCreateRoomEntity.getMtHotelId() + "_" + switchCreateRoomEntity.getMtGoodId();
                                if(redisService.exists(key)){
                                    redisService.remove(key);
                                }
                                String key2=CommonConstants.SWITCH_CREATE_ROOM_BREAKFAST+switchCreateRoomEntity.getMtHotelId()+"_"+switchCreateRoomEntity.getMtGoodId()+"_1";
                                if(redisService.exists(key2)){
                                    redisService.remove(key2);
                                }
                                key2=CommonConstants.SWITCH_CREATE_ROOM_BREAKFAST+switchCreateRoomEntity.getMtHotelId()+"_"+switchCreateRoomEntity.getMtGoodId()+"_0";
                                if(redisService.exists(key2)){
                                    redisService.remove(key2);
                                }
                                String keys=CommonConstants.SWITCH_SELLING_REDIS_BY_MT+switchCreateRoomEntity.getMtHotelId()+"_"+switchCreateRoomEntity.getMtGoodId();
                                if(redisService.exists(keys)){
                                    redisService.remove(keys);
                                }
                                //清除取消规则
                                keys=CommonConstants.SWITCH_CREATE_ROOM_CANCEL_RULE+switchCreateRoomEntity.getMtHotelId()+"_"+switchCreateRoomEntity.getMtGoodId()+"_0";
                                if(redisService.exists(keys)){
                                    redisService.remove(keys);
                                }
                                keys=CommonConstants.SWITCH_CREATE_ROOM_CANCEL_RULE+switchCreateRoomEntity.getMtHotelId()+"_"+switchCreateRoomEntity.getMtGoodId()+"_1";
                                if(redisService.exists(keys)){
                                    redisService.remove(keys);
                                }
                            }
                            //5.更新创建子物理房型
                            switchCreateBasicRoomService.updateCreateStateByBasicRoomId(switchCreateRoomEntity.getBasicRoomTypeId());
                        }
                    }
                }
            }
        }
    }

    /**
     *
     * @author: lmf
     * @time: 2020/10/28 16:38
     * 美团新增产品
     * @param productNewAddVo
     */
    @Override
    public Result<Object> productNewAdd(ProductNewAddVo productNewAddVo) {
        List<MtHotelGoodsDataGroupRoomDto>  mtHotelGoodsDataGroupRoomDtos=productNewAddVo.getMtRoomGoodsData();
        if(ObjectUtils.isEmpty(mtHotelGoodsDataGroupRoomDtos)){
            return new Result<>();
        }
        int mtHotelId;
        int mtRoomId;
        SwitchCreateRoomEntity switchCreateRoomEntity;
        SwitchCreateRoomFailureEntity switchCreateRoomFailureEntity;
        tab:for(MtHotelGoodsDataGroupRoomDto mtHotelGoodsDataGroupRoomDto:mtHotelGoodsDataGroupRoomDtos){
            log.info("===========美团新增产品的信息为{}",mtHotelGoodsDataGroupRoomDto);
            mtHotelId=Integer.parseInt(mtHotelGoodsDataGroupRoomDto.getMtHotelId());
            mtRoomId=Integer.parseInt(mtHotelGoodsDataGroupRoomDto.getMtRoomId());
            //说明此房型没有失效房型
            int up=0;
            switchCreateRoomFailureEntity=switchCreateRoomFailureMapper.selectMtHotelIdAndMtRoomId(mtHotelId,mtRoomId);
            if(ObjectUtil.isEmpty(switchCreateRoomFailureEntity)){
                switchCreateRoomEntity=switchCreateRoomMapper.selectMtHotelIdAndMtRoomId(mtHotelId,mtRoomId);
                //说明此房型没有售卖房型
                if(ObjectUtils.isEmpty(switchCreateRoomEntity)){
                   //判断该房型是否有创建过子物理房型
                    String url=CommonUrlConstants.COMPARE_HOST+CompareRequestUrlEnums.GET_HOTEL_BASIC_ROOM_INFO_BY_MT_HOTEL_ID_AND_MT_ROOM_ID.getUrl();
                    Map<String,String> params=new HashMap<>();
                    params.put("mtHotelId",mtHotelGoodsDataGroupRoomDto.getMtHotelId());
                    params.put("mtRoomId",mtHotelGoodsDataGroupRoomDto.getMtRoomId());
                    Result<CompareHotelBasicRoom> result=switchRestTempaleUtilsService.sendPostRequestToCompareToGetRoomInfoByMtHotelIdAndMtRoomId(url,params);
                    CompareHotelBasicRoom compareHotelBasicRoom=result.getData();
                    if(ObjectUtil.isEmpty(compareHotelBasicRoom)){
                        continue tab;
                    }else{//从创建子物理表获取子物理房型id,更新创建子物理房型的状态
                        Example example=new Example(SwitchHotelUpEntity.class);
                        Example.Criteria criteria=example.createCriteria();
                        criteria.andEqualTo("hotelId",compareHotelBasicRoom.getSwitchHotelId());
                        //黑名单的酒店不进行创建,且不存在上架记录直接设置为无效
                        SwitchHotelUpEntity switchHotelUpEntity=switchHotelUpMapper.selectOneByExample(example);
                        //黑名单的酒店不进行创建产品
                        if(ObjectUtil.isEmpty(switchHotelUpEntity)||switchHotelUpEntity.getUp()==2||switchHotelUpEntity.getStatus()==2){
                            continue tab;
                        }else{
                            Result<Object> setHotelSupplierBookableResult=new Result<>();
                            //只有售卖房型才进行售卖
                            up=1;
                            if(switchHotelUpEntity.getUp()==3){
                                //缓存有key,就不唤起售卖酒店在售状态
                                if(redisService.exists("switch_create_room_hotel_control_up")){
                                    continue ;
                                }else{
                                    up=3;
                                    //设置上线
                                    Map<String, Object> updateChannelVoMap = new HashMap<>();
                                    updateChannelVoMap.put("hotelIDs", new int[]{compareHotelBasicRoom.getSwitchHotelId()});
                                    updateChannelVoMap.put("cBookable", 1);
                                    updateChannelVoMap.put("qBookable", 1);
                                    updateChannelVoMap.put("channelABookable",1);//默认停售
                                    updateChannelVoMap.put("b2bBookable", 1);//默认停售
                                    setHotelSupplierBookableResult = switchSubHotelInfoService.setHotelSupplierBookable(updateChannelVoMap, new ArrayList<SupplierTimeSaleInfoVo>());
                                    if(setHotelSupplierBookableResult.getMsg().toString().contains("频繁")){
                                        try {
                                            Thread.sleep(6000);
                                            setHotelSupplierBookableResult = switchSubHotelInfoService.setHotelSupplierBookable(updateChannelVoMap, new ArrayList<SupplierTimeSaleInfoVo>());
                                        } catch (InterruptedException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                }
                            }else{
                                Map<String, Object> updateChannelVoMap = new HashMap<>();
                                updateChannelVoMap.put("hotelIDs", new int[]{compareHotelBasicRoom.getSwitchHotelId()});
                                updateChannelVoMap.put("cBookable", 0);
                                updateChannelVoMap.put("qBookable", 1);
                                updateChannelVoMap.put("channelABookable",1);//默认停售
                                updateChannelVoMap.put("b2bBookable", 1);//默认停售
                                setHotelSupplierBookableResult = switchSubHotelInfoService.setHotelSupplierBookable(updateChannelVoMap, new ArrayList<SupplierTimeSaleInfoVo>());
                                if(setHotelSupplierBookableResult.getMsg().toString().contains("频繁")){
                                    try {
                                        Thread.sleep(6000);
                                        setHotelSupplierBookableResult = switchSubHotelInfoService.setHotelSupplierBookable(updateChannelVoMap, new ArrayList<SupplierTimeSaleInfoVo>());
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }
                            if(setHotelSupplierBookableResult.getCode()==CommonConstants.SUCCESS){
                                List<Integer> list=new ArrayList<>();
                                list.add(compareHotelBasicRoom.getSwitchHotelId());
                                List<SwitchCreateRoomEntity> switchCreateRoomEntities = switchCreateRoomMapper.selectById(list);
                                for (SwitchCreateRoomEntity switchCreateRoomEntitys : switchCreateRoomEntities) {
                                    String key = CommonConstants.SWITCH_SELLING_THE_HOUSE_TYPE + switchCreateRoomEntitys.getMtHotelId() + "_" + switchCreateRoomEntitys.getMtGoodId();
                                    redisService.set(key,switchCreateRoomEntitys.getMtRoomId());
                                    String key2 = CommonConstants.SWITCH_SELLING_THE_HOUSE_ROOM_TYPE + switchCreateRoomEntitys.getMtHotelId() + "_" + switchCreateRoomEntitys.getMtRoomId();
                                    redisService.set(key2,1);
                                    //设置餐食
                                    if(switchCreateRoomEntitys.getRateCodePropertyValueIdList().contains("498160")){
                                        redisService.set(CommonConstants.SWITCH_CREATE_ROOM_BREAKFAST+"_0",0);
                                    }else if(switchCreateRoomEntitys.getRateCodePropertyValueIdList().contains("640292")){
                                        redisService.set(CommonConstants.SWITCH_CREATE_ROOM_BREAKFAST+"_1",2);
                                    }else{
                                        redisService.set(CommonConstants.SWITCH_CREATE_ROOM_BREAKFAST+"_1",switchCreateRoomEntitys.getMealCount());
                                    }
                                    String key4=CommonConstants.SWITCH_SELLING_REDIS_BY_MT+switchCreateRoomEntitys.getMtHotelId() + "_" + switchCreateRoomEntitys.getMtGoodId();
                                    if(redisService.exists(key4)){
                                        redisService.remove(key4);
                                    }
                                    int day=3;
                                    //删除价格和房态缓存
                                    for(int i=0;i<day;i++){
                                        //删除房态
                                        key=CallbackConstants.REDIS_KEY_CALLBACK_CHANGE_STATUS_HEAD+switchCreateRoomEntitys.getMtHotelId()+"_"+switchCreateRoomEntitys.getMtGoodId()+"_"+DateUtil.getDateAfter(new Date(),i);
                                        if(redisService.exists(key)){
                                            redisService.remove(key);
                                        }
                                        //删除价格
                                        key=CallbackConstants.REDIS_KEY_CALLBACK_CHANGE_PRICE_HEAD+switchCreateRoomEntitys.getMtHotelId()+"_"+switchCreateRoomEntitys.getMtGoodId()+"_"+DateUtil.getDateAfter(new Date(),i);
                                        if(redisService.exists(key)){
                                            redisService.remove(key);
                                        }
                                    }
                                    key=CallbackConstants.REDIS_ROW_GOODS_PRICE_CHECK+switchCreateRoomEntitys.getMtHotelId()+"_"+switchCreateRoomEntitys.getMtGoodId();
                                    if(redisService.exists(key)){
                                        log.info("======key {}删除中=========",key);
                                        redisService.remove(key);
                                    }
                                    key=CallbackConstants.REDIS_ROW_GOODS_STATUS_CHECK+switchCreateRoomEntitys.getMtHotelId()+"_"+switchCreateRoomEntitys.getMtGoodId();
                                    if(redisService.exists(key)){
                                        log.info("======key {}删除中=========",key);
                                        redisService.remove(key);
                                    }
                                }
                                switchHotelUpMapper.updateHotelStatus(compareHotelBasicRoom.getSwitchHotelId(),up);
                                SwitchHotelUpLogEntity switchHotelUpLogEntity=new SwitchHotelUpLogEntity();
                                switchHotelUpLogEntity.setCreateTime(new Date());
                                switchHotelUpLogEntity.setGenre(0);
                                switchHotelUpLogEntity.setHotelId(compareHotelBasicRoom.getSwitchHotelId());
                                switchHotelUpLogEntity.setUp(up);
                                switchHotelUpLogEntity.setUsername("system-新增产品上架");
                                switchHotelUpLogMapper.insert(switchHotelUpLogEntity);
                            }

                        }
                       switchCreateBasicRoomService.updateByHotelIdAndMasterBasicRoomId(compareHotelBasicRoom.getSwitchHotelId(),compareHotelBasicRoom.getSwitchHotelRoomId());
                    }
                }else {///该产品已经创建过后，且无过期的产品数据
                    addCreateRoom(mtHotelGoodsDataGroupRoomDto,switchCreateRoomEntity.getHotelId(),switchCreateRoomEntity.getBasicRoomTypeId(), mtRoomId,switchCreateRoomEntity.getMasterHotelId(), mtHotelId,0);
                }
            }else{//该产品已经创建过后，且有过期的产品数据
                addCreateRoom(mtHotelGoodsDataGroupRoomDto,switchCreateRoomFailureEntity.getHotelId(),switchCreateRoomFailureEntity.getBasicRoomTypeId(), mtRoomId,switchCreateRoomFailureEntity.getMasterHotelId(), mtHotelId,1);
            }
        }
        return new Result<>();
    }
    /**
     * 创建新产品接口
     */
    public Result addCreateRoom(MtHotelGoodsDataGroupRoomDto mtHotelGoodsDataGroupRoomDto,Integer hotelId,Integer basicRoomTypeId,Integer mtRoomId,Integer masterHotelId,Integer mtHotelId,Integer genre){
        Example example=new Example(SwitchHotelUpEntity.class);
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("hotelId",hotelId);
        //黑名单的酒店不进行创建
        SwitchHotelUpEntity switchHotelUpEntity=switchHotelUpMapper.selectOneByExample(example);
        if(ObjectUtil.isEmpty(switchHotelUpEntity)||switchHotelUpEntity.getUp()==2||switchHotelUpEntity.getStatus()==2){
            Map<String, Object> updateChannelVoMap = new HashMap<>();
            updateChannelVoMap.put("hotelIDs", new int[]{hotelId});
            updateChannelVoMap.put("cBookable", 0);
            updateChannelVoMap.put("qBookable", 0);
            updateChannelVoMap.put("channelABookable",0);//默认停售
            updateChannelVoMap.put("b2bBookable", 0);//默认停售
            Result<Object> setHotelSupplierBookableResult = switchSubHotelInfoService.setHotelSupplierBookable(updateChannelVoMap, new ArrayList<SupplierTimeSaleInfoVo>());
            if(setHotelSupplierBookableResult.getCode()==CommonConstants.SUCCESS){
                List<Integer> list=new ArrayList<>();
                list.add(hotelId);
                List<SwitchCreateRoomEntity> switchCreateRoomEntities = switchCreateRoomMapper.selectById(list);
                for (SwitchCreateRoomEntity switchCreateRoomEntitys : switchCreateRoomEntities) {
                    String key = CommonConstants.SWITCH_SELLING_THE_HOUSE_TYPE + switchCreateRoomEntitys.getMtHotelId() + "_" + switchCreateRoomEntitys.getMtGoodId();
                    if(redisService.exists(key)){
                        redisService.remove(key);
                    }
                    String key2 = CommonConstants.SWITCH_SELLING_THE_HOUSE_ROOM_TYPE + switchCreateRoomEntitys.getMtHotelId() + "_" + switchCreateRoomEntitys.getMtRoomId();
                    if(redisService.exists(key2)){
                        redisService.remove(key2);
                    }
                }
                switchHotelUpMapper.updateHotelStatus(hotelId,1);
                SwitchHotelUpLogEntity switchHotelUpLogEntity=new SwitchHotelUpLogEntity();
                switchHotelUpLogEntity.setCreateTime(new Date());
                switchHotelUpLogEntity.setGenre(0);
                switchHotelUpLogEntity.setHotelId(hotelId);
                switchHotelUpLogEntity.setUp(2);
                switchHotelUpLogEntity.setUsername("system-新增产品上架-黑名单酒店设置下线");
                switchHotelUpLogMapper.insert(switchHotelUpLogEntity);
            }
            return null;
        }else{
            //设置上线
            Map<String, Object> updateChannelVoMap = new HashMap<>();
            updateChannelVoMap.put("hotelIDs", new int[]{hotelId});
            int up=1;
            if(switchHotelUpEntity.getUp()==3){
                up=3;
                updateChannelVoMap.put("cBookable", 1);
            }else{
                updateChannelVoMap.put("cBookable", 0);
            }
            updateChannelVoMap.put("qBookable", 1);
            updateChannelVoMap.put("channelABookable",1);//默认停售
            updateChannelVoMap.put("b2bBookable", 1);//默认停售
            Result<Object> setHotelSupplierBookableResult = switchSubHotelInfoService.setHotelSupplierBookable(updateChannelVoMap, new ArrayList<SupplierTimeSaleInfoVo>());
            if(setHotelSupplierBookableResult.getCode()==CommonConstants.SUCCESS){
                switchHotelUpMapper.updateHotelStatus(hotelId,up);
                SwitchHotelUpLogEntity switchHotelUpLogEntity=new SwitchHotelUpLogEntity();
                switchHotelUpLogEntity.setCreateTime(new Date());
                switchHotelUpLogEntity.setGenre(0);
                switchHotelUpLogEntity.setHotelId(hotelId);
                switchHotelUpLogEntity.setUp(up);
                switchHotelUpLogEntity.setUsername("system-新增产品上架");
                switchHotelUpLogMapper.insert(switchHotelUpLogEntity);
            }
            List<MtHotelGoodsDto> mtHotelGoodsDtoData=mtHotelGoodsDataGroupRoomDto.getMtHotelGoodsDtoData();
            CreateRoomVo createRoomVo;
            List<Integer> rateCodePropertyValueIDList;
            int breakFast;
            Map<String, Integer> map;
            SwitchLableAddEnums switchLableEnums;
            CreateRoomResultVo createRoomResultVo;
            Result<CreateRoomResultVo> resultRoom;
            SwitchCreateRoomEntity switchCreateRoomByMt;
            SwitchCreateRoomEntity switchCreateRoomEntityData;
            switchTab:for(MtHotelGoodsDto mtHotelGoodsDto:mtHotelGoodsDtoData){
                //如果产品是钟点房 不创建 房态是不可预定(推送的话就会做判断)
                //過濾掉不可展示的產品 mtHotelGoodsDto.getMtGoodsStatus()==3
                if(mtHotelGoodsDto.getMtGoodsStatus()==3) {
                    continue switchTab;
                }
                //过滤钟点房
                if (mtHotelGoodsDto.getMtGoodsType() ==2) {
                    continue switchTab;
                }
                //过滤掉需要身份证验证的
                if(mtHotelGoodsDto.getMtNeedIdentityCard()==1){
                    continue switchTab;
                }
                //过滤掉第三方开票方
                MtHotelGoodsInvoiceDto mtHotelGoodsInvoiceDto=mtHotelGoodsDto.getInvoice();
                if(!ObjectUtils.isEmpty(mtHotelGoodsInvoiceDto)){
                    if(mtHotelGoodsInvoiceDto.getInvoiceMode()==3||mtHotelGoodsInvoiceDto.getInvoiceMode()==2){
                        continue switchTab;
                    }
                }
                /**
                 * 过滤掉需要连住的产品
                 * 如果最少入住天数>1 产品暂时不要
                 * 如果最少预定间数>1 产品暂时不要
                 */
                List<MtHotelGoodsBookRuleDto> mtHotelGoodsBookRuleDtos=mtHotelGoodsDto.getBookRuleList();
                if(!ObjectUtil.isEmpty(mtHotelGoodsBookRuleDtos)&&mtHotelGoodsBookRuleDtos.size()>0){
                    for(MtHotelGoodsBookRuleDto mtHotelGoodsBookRuleDto:mtHotelGoodsBookRuleDtos){
                        if(mtHotelGoodsBookRuleDto.getMtSerialCheckinMin()>1||mtHotelGoodsBookRuleDto.getMtRoomCountMin()>1){
                            continue switchTab;
                        }
                    }
                }
                //过滤掉产品名称需要证件的产品
                if(mtHotelGoodsDto.getMtGoodsName().contains("证件")){
                    continue switchTab;
                }
                if(mtHotelGoodsDto.getMtGoodsName().contains("学生证")){
                    continue switchTab;
                }
                if(mtHotelGoodsDto.getMtGoodsName().contains("护士证")){
                    continue switchTab;
                }
                if(mtHotelGoodsDto.getMtGoodsName().contains("资格证")){
                    continue switchTab;
                }
                if(mtHotelGoodsDto.getMtGoodsName().contains("户籍")){
                    continue switchTab;
                }
                if(mtHotelGoodsDto.getMtGoodsName().contains("零点后入住")){
                    continue switchTab;
                }
                log.info("======房型为mtRoomId{},对应的产品列表为{}",mtRoomId, JSON.toJSONString(mtHotelGoodsDtoData));
                //封装调用创建售卖房型接口请求参数
                createRoomVo = new CreateRoomVo();
                //5.1 设置售卖房型信息
                //设置子酒店
                createRoomVo.setHotelId(hotelId);
                //设置子物理房型id
                createRoomVo.setBasicRoomTypeId(basicRoomTypeId);
                //5.2.对产品名称进行拆分 把餐食规则 入住人数 分出来
                rateCodePropertyValueIDList = new ArrayList<>();
                synchronized (this){
                    map = getBreakFastId(mtHotelGoodsDto.getBreakfastList());
                }
                Map<String, Integer> map2=new HashMap<>();
                synchronized (this){
                    map2 = getBreakFastId(mtHotelGoodsDto.getBreakfastList());
                }
                breakFast = map.get("breakId");
                int breakFast2=map2.get("breakId");
                int meanCount=map.get("mealCount");
                //餐食做二次效验
                if(breakFast!=breakFast2){
                    continue switchTab;
                }
                log.info("==========子物理房型为{}根据默认的早餐规则获取的餐食类型为{}",basicRoomTypeId,breakFast);
                if (breakFast == 8) {
                    rateCodePropertyValueIDList.add((breakFast < 1) ? SwitchBreakFastEnums.NO.getCode() : breakFast);
                    createRoomVo.setMealCount(map.get("mealCount"));
                } else {
                    rateCodePropertyValueIDList.add((breakFast < 1) ? SwitchBreakFastEnums.NO.getCode() : breakFast);
                }
                //5.3添加促销标签 随机获取某个标签
                switchLableEnums = RandomEnums.random(SwitchLableAddEnums.class);
                rateCodePropertyValueIDList.add(switchLableEnums.getCode());
                createRoomVo.setRateCodePropertyValueIDList(rateCodePropertyValueIDList.stream().toArray(Integer[]::new));
                //如果美團的產品id+酒店id有對應的映射過濾
                switchCreateRoomByMt = switchCreateRoomMapper.selectByMtGoodsIdAndHotelId(Integer.valueOf(mtHotelGoodsDto.getMtHotelId()), Integer.valueOf(mtHotelGoodsDto.getMtGoodsId()));
                if(!ObjectUtils.isEmpty(switchCreateRoomByMt)){
                    log.info("========美团的酒店id为{},美团的产品id为{}=======",mtHotelGoodsDto.getMtHotelId(),mtHotelGoodsDto.getMtGoodsId());
                    continue switchTab;
                }
                //判断创建物理房型表是否有数据
                SwitchBasicRoomType switchBasicRoomType=switchBasicRoomTypeService.selectByBasicId(hotelId,basicRoomTypeId);
                if(ObjectUtil.isEmpty(switchBasicRoomType)){
                    log.info("==========不存在此物理房型数据=============");
                    continue switchTab;
                }
                //到效验表里获取数据
                String url=CommonUrlConstants.COMPARE_HOST+CompareRequestUrlEnums.GET_HOTEL_BASIC_ROOM_INFO_BY_SIWTCH_ROOM_ID_AND_MT_ROOM_ID.getUrl();
                Map<String,Integer> params=new HashMap<>();
                params.put("switchRoomId",switchBasicRoomType.getMasterBasicRoomId());
                params.put("mtRoomId",mtRoomId);
                Result<CompareHotelBasicRoom> result=switchRestTempaleUtilsService.sendPostRequestToCompareToGetRoomInfo(url,params);
                if(result.getCode()!=CommonConstants.SUCCESS){
                    continue switchTab;
                }
                CompareHotelBasicRoom compareHotelBasicRoom=result.getData();
                //无对比数据也不进行创建
                if(ObjectUtil.isEmpty(compareHotelBasicRoom)){
                    continue switchTab;
                }
                if(!Integer.valueOf(mtHotelGoodsDto.getMtRoomId()).equals(mtRoomId)){
                    log.info("=========美团的房型id为{}和产品的房型id{},不一样不进行创建==",mtRoomId,mtHotelGoodsDto.getMtRoomId());
                    continue switchTab;
                }
                //7.调用创建接口
                resultRoom = switchBasicRoomTypeService.createRoom(createRoomVo);
                log.info("==========调用创建售卖房型接口返回数据为{}======",resultRoom);
                if (resultRoom.getCode() == CommonConstants.SUCCESS) {
                    //封装创建售卖房型数据
                    switchCreateRoomEntityData = new SwitchCreateRoomEntity();
                    switchCreateRoomEntityData.setId(StringUtil.getUUID());
                    switchCreateRoomEntityData.setHotelId(hotelId);
                    switchCreateRoomEntityData.setBasicRoomTypeId(basicRoomTypeId);
                    switchCreateRoomEntityData.setRoomTypeId(0);
                    switchCreateRoomEntityData.setMasterHotelId(masterHotelId);
                    switchCreateRoomEntityData.setRelationRoomId(0);
                    switchCreateRoomEntityData.setRateCodePropertyValueIdList(StringUtil.getStringByList(rateCodePropertyValueIDList, ","));
                    switchCreateRoomEntityData.setMealCount(meanCount);
                    switchCreateRoomEntityData.setApplicabilityInfo(createRoomVo.getApplicabilityInfo());
                    switchCreateRoomEntityData.setApplicabilityList(StringUtil.getStringByArray(createRoomVo.getApplicabilityList(), ","));
                    switchCreateRoomEntityData.setPriceSystemName(ObjectUtils.isEmpty(createRoomVo.getPriceSystemName()) ? "" : createRoomVo.getPriceSystemName());
                    switchCreateRoomEntityData.setBlackChannelList(StringUtil.getStringByArray(createRoomVo.getBlackChannelList(), ","));
                    switchCreateRoomEntityData.setMtHotelId(mtHotelId);
                    switchCreateRoomEntityData.setMtGoodId(Integer.valueOf(mtHotelGoodsDto.getMtGoodsId()));
                    switchCreateRoomEntityData.setState(0);
                    switchCreateRoomEntityData.setUp(0);
                    switchCreateRoomEntityData.setRemark("");
                    switchCreateRoomEntityData.setCreateTime(new Date());
                    switchCreateRoomEntityData.setMtGoodName(mtHotelGoodsDto.getMtGoodsName());
                    switchCreateRoomEntityData.setModifyTime(new Date());
                    switchCreateRoomEntityData.setMtRoomId(mtHotelGoodsDto.getMtRoomId());
                    switchCreateRoomEntityData.setMtRoomName(mtHotelGoodsDto.getMtRoomName());
                    createRoomResultVo = resultRoom.getData();
                    switchCreateRoomEntityData.setRoomId(createRoomResultVo.getRoomTypeId());
                    switchCreateRoomEntityData.setSwitchRoomName(compareHotelBasicRoom.getSwitchRoomName());
                    if(createRoomResultVo.getRoomTypeId()<1){
                        continue switchTab;
                    }
                    //说明有过期商品
                    if(genre==1){
                        if(StringUtil.isBlank(createRoomResultVo.getRoomName())){
                            //从失效表获取售卖房型名称
                            SwitchCreateRoomFailureEntity switchCreateRoomFailureEntity=switchCreateRoomFailureMapper.selectRoomNameByHotelIdAndRoomId(hotelId,createRoomResultVo.getRoomTypeId());
                            if(!ObjectUtil.isEmpty(switchCreateRoomFailureEntity)){
                                switchCreateRoomEntityData.setRoomName(switchCreateRoomFailureEntity.getRoomName());
                            }else{//获取静态信息获取
                                //获取母酒店id
                                Map<String,Object> map1=new HashMap<>();
                                map1.put("hotelId",hotelId);
                                String[] types=new String[]{"RoomTypeSimpleEntity"};
                                map1.put("returnDataTypeList",types);
                                String strPro = SwitchsHttpUtils.sendRequest(map1, "gethotelroomstaticinfo", profile);
                                //对结构进行格式化
                                RoomStaticInfosVo roomStaticInfosVo = null;
                                Map<String,Object> parse = JacksonUtil.parse(strPro,Map.class);
                                String retJson = JacksonUtil.objectToJson(parse.get("roomStaticInfos"));
                                roomStaticInfosVo =JacksonUtil.parse(retJson, RoomStaticInfosVo.class);
                                List<RoomInfoVo>  roomInfoVos=roomStaticInfosVo.getRoomStaticInfo().get(0).getRoomInfos().getRoomInfo();
                                if(!ObjectUtils.isEmpty(roomInfoVos)&&roomInfoVos.size()>0){
                                    for(RoomInfoVo roomInfoVo:roomInfoVos) {
                                        if (roomInfoVo.getRoomId().equals(createRoomResultVo.getRoomTypeId())) {
                                            switchCreateRoomEntityData.setRoomName(roomInfoVo.getRoomName());
                                        }
                                    }
                                }
                            }
                        }else{
                            switchCreateRoomEntityData.setRoomName(createRoomResultVo.getRoomName());
                        }
                    }else{
                        switchCreateRoomEntityData.setRoomName(createRoomResultVo.getRoomName());
                    }
                    Example example1=new Example(SwitchCreateRoomFailureEntity.class);
                    Example.Criteria criteria1=example1.createCriteria();
                    criteria1.andEqualTo("roomId",createRoomResultVo.getRoomTypeId());
                    List<SwitchCreateRoomFailureEntity> list=switchCreateRoomFailureMapper.selectByExample(example1);
                    if(!ObjectUtils.isEmpty(list)||list.size()>1){
                        SetRoomOnlineOfflineVo setRoomOnlineOfflineVo=new SetRoomOnlineOfflineVo();
                        setRoomOnlineOfflineVo.setRoomTypeId(createRoomResultVo.getRoomTypeId());
                        setRoomOnlineOfflineVo.setHotelId(hotelId);
                        setRoomOnlineOfflineVo.setChannel("Ctrip");
                        setRoomOnlineOfflineVo.setRoomStatus(1);
                        switchBasicRoomTypeService.setRoomOnlineOffline(setRoomOnlineOfflineVo);
                    }
                  //  setRoomOnlineOfflineVo.setChannel("Qunar");
                 //   switchBasicRoomTypeService.setRoomOnlineOffline(setRoomOnlineOfflineVo);
                    //如果售賣房型id+子酒店id 創建過
                    SwitchCreateRoomEntity switchCreateRoom = switchCreateRoomMapper.selectByRoomIdAndHotelId(hotelId, createRoomResultVo.getRoomTypeId());
                    if(!ObjectUtils.isEmpty(switchCreateRoom)){
                        continue switchTab;
                    }
                    String key = CommonConstants.SWITCH_SELLING_THE_HOUSE_TYPE + mtHotelId + "_" + mtHotelGoodsDto.getMtGoodsId();
                    if(breakFast!=498160){
                        String key2=CommonConstants.SWITCH_CREATE_ROOM_BREAKFAST+ mtHotelId + "_" + mtHotelGoodsDto.getMtGoodsId()+"_1";
                        redisService.set(key2,meanCount);
                    }else{
                        String key2=CommonConstants.SWITCH_CREATE_ROOM_BREAKFAST+ mtHotelId + "_" + mtHotelGoodsDto.getMtGoodsId()+"_0";
                        redisService.set(key2,0);
                    }
                    String key3=CommonConstants.SWITCH_SELLING_REDIS_BY_MT+mtHotelId+"_"+mtHotelGoodsDto.getMtGoodsId();
                    if(redisService.exists(key3)){
                        redisService.remove(key3);
                    }
                    redisService.set(key,"1");
                    int day=3;
                    //删除价格和房态缓存
                    for(int i=0;i<day;i++){
                        //删除房态
                        key=CallbackConstants.REDIS_KEY_CALLBACK_CHANGE_STATUS_HEAD+mtHotelId+"_"+mtHotelGoodsDto.getMtGoodsId()+"_"+DateUtil.getDateAfter(new Date(),i);
                        if(redisService.exists(key)){
                            redisService.remove(key);
                        }
                        //删除价格
                        key=CallbackConstants.REDIS_KEY_CALLBACK_CHANGE_PRICE_HEAD+mtHotelId+"_"+mtHotelGoodsDto.getMtGoodsId()+"_"+DateUtil.getDateAfter(new Date(),i);
                        if(redisService.exists(key)){
                            redisService.remove(key);
                        }
                    }
                    key=CallbackConstants.REDIS_ROW_GOODS_PRICE_CHECK+mtHotelId+"_"+mtHotelGoodsDto.getMtGoodsId();
                    if(redisService.exists(key)){
                        log.info("======key {}删除中=========",key);
                        redisService.remove(key);
                    }
                    key=CallbackConstants.REDIS_ROW_GOODS_STATUS_CHECK+mtHotelId+"_"+mtHotelGoodsDto.getMtGoodsId();
                    if(redisService.exists(key)){
                        log.info("======key {}删除中=========",key);
                        redisService.remove(key);
                    }
                    //删除取消规则 SWITCH_CREATE_ROOM_CANCEL_RULE
                    key=CommonConstants.SWITCH_CREATE_ROOM_CANCEL_RULE+mtHotelId+"_"+mtHotelGoodsDto.getMtGoodsId()+"_1";
                    if(redisService.exists(key)){
                        redisService.remove(key);
                    }
                    key=CommonConstants.SWITCH_CREATE_ROOM_CANCEL_RULE+mtHotelId+"_"+mtHotelGoodsDto.getMtGoodsId()+"_0";
                    if(redisService.exists(key)){
                        redisService.remove(key);
                    }
                    //设置取消规则
                    List<MtHotelGoodsCancelRuleDto> mtHotelGoodsCancelRuleDtos=mtHotelGoodsDto.getCancelRuleList();
                    if(ObjectUtils.isEmpty(mtHotelGoodsCancelRuleDtos)||mtHotelGoodsCancelRuleDtos.size()<1){
                        key=CommonConstants.SWITCH_CREATE_ROOM_CANCEL_RULE+mtHotelId+"_"+mtHotelGoodsDto.getMtGoodsId()+"_0";
                        redisService.set(key,"0");
                    }else{
                        for(MtHotelGoodsCancelRuleDto mtHotelGoodsCancelRuleVo:mtHotelGoodsCancelRuleDtos){
                            //不可取消
                            if(mtHotelGoodsCancelRuleVo.getMtCancelType()==0){
                                key=CommonConstants.SWITCH_CREATE_ROOM_CANCEL_RULE+mtHotelId+"_"+mtHotelGoodsDto.getMtGoodsId()+"_0";
                                redisService.set(key,"0");
                            }else{
                                key=CommonConstants.SWITCH_CREATE_ROOM_CANCEL_RULE+mtHotelId+"_"+mtHotelGoodsDto.getMtGoodsId()+"_1";
                                String time="";
                                time=mtHotelGoodsCancelRuleVo.getMtAheadCancelDays()+"_"+mtHotelGoodsCancelRuleVo.getMtAheadCancelHours();
                                redisService.set(key,time);
                            }
                        }
                    }
                    switchCreateRoomMapper.insert(switchCreateRoomEntityData);
                    //设置房态库存共享
                    setRoomShare(hotelId, createRoomResultVo.getRoomTypeId(), SwitchChannelNameEnums.CTRIP.getName());
                    setRoomShare(hotelId, createRoomResultVo.getRoomTypeId(), SwitchChannelNameEnums.B2B.getName());
                    //设置房型售卖规则
                    log.info("============获取产品的取消规则{}====================", mtHotelGoodsDto.getCancelRuleList().size());
                    setRoomSaleRule(hotelId, createRoomResultVo.getRoomTypeId(), createRoomResultVo.getRoomName(), mtHotelGoodsDto.getCancelRuleList(), mtHotelGoodsDto.getBookRuleList());
                    log.info("=======创建售卖房型完成===============");
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    log.info("=======创建售卖房型失败===============,原因{}", resultRoom.getMsg());
                }
            }
            return new Result();
        }

    }
    /**
     *
     * @author: lmf
     * @time: 2020/10/29 10:01
     * 跑失效的产品重新找出满足条件的售卖房型列表
     */
    @Override
    public Result<Object> updateCreateRoomBreakAgein() {
        return new Result<>();
    }

    /**
     * 美团房型失效
     * @param mtHotelRoomDownVo
     * @return
     */
    @Override
    @Transactional
    public Result<Object> roomFailure(MtHotelRoomDownVo mtHotelRoomDownVo) {
        ExecutorService executorService = Executors.newFixedThreadPool(1);
        executorService.submit(new RoomFailureThread(mtHotelRoomDownVo));
        executorService.shutdown();
        return new Result<>();
    }


    class RoomFailureThread implements Runnable{
        private MtHotelRoomDownVo mtHotelRoomDownVo;

        public RoomFailureThread(MtHotelRoomDownVo mtHotelRoomDownVo) {
            this.mtHotelRoomDownVo = mtHotelRoomDownVo;
        }
        @Override
        public void run() {
            int mtHotelId=mtHotelRoomDownVo.getMtHotelId();
            tab:for(MtHotelRoomDeatilVo mtHotelRoomDeatilVo:mtHotelRoomDownVo.getMtRoomList()){
                SwitchRoomFailureLogEntity switchRoomFailureLogEntity=new SwitchRoomFailureLogEntity();
                switchRoomFailureLogEntity.setMtHotelId(mtHotelId);
                if(mtHotelRoomDeatilVo.getGenre()==1){
                    switchRoomFailureLogEntity.setCreateTime(new Date());
                    switchRoomFailureLogEntity.setGenre(mtHotelRoomDeatilVo.getGenre());
                    switchRoomFailureLogEntity.setModifyTime(new Date());
                    switchRoomFailureLogEntity.setMtRoomId(mtHotelRoomDeatilVo.getMtRoomId());
                    switchRoomFailureLogEntity.setRemark("");
                    switchRoomFailureLogMapper.insert(switchRoomFailureLogEntity);
                    continue tab;
                }
                List<SwitchCreateRoomEntity> switchCreateRoomEntities=switchCreateRoomMapper.selectRoomInfoByMtHotelIdAndMtHotelRoomId(mtHotelId,mtHotelRoomDeatilVo.getMtRoomId());
                if(ObjectUtil.isEmpty(switchCreateRoomEntities)||switchCreateRoomEntities.size()<1){
                    continue tab;
                }
                for(SwitchCreateRoomEntity switchCreateRoomEntity:switchCreateRoomEntities){
                    //1.首先把这个房型下的产品都设置为失效
                    SetRoomOnlineOfflineVo setRoomOnlineOfflineVo = new SetRoomOnlineOfflineVo();
                    setRoomOnlineOfflineVo.setRoomTypeId(switchCreateRoomEntity.getRoomId());
                    setRoomOnlineOfflineVo.setHotelId(switchCreateRoomEntity.getHotelId());
                    setRoomOnlineOfflineVo.setChannel("Ctrip");
                    setRoomOnlineOfflineVo.setRoomStatus(2);
                    Result<Object> result = switchBasicRoomTypeService.setRoomOnlineOffline(setRoomOnlineOfflineVo);
                  //  setRoomOnlineOfflineVo.setChannel("Qunar");
                 //   switchBasicRoomTypeService.setRoomOnlineOffline(setRoomOnlineOfflineVo);
                    String sellingKey=CommonConstants.SWITCH_SELLING_REDIS_BY_MT+switchCreateRoomEntity.getMtHotelId()+"_"+switchCreateRoomEntity.getMtGoodId();
                    if(redisService.exists(sellingKey)){
                        redisService.remove(sellingKey);
                    }
                    if(result.getCode()==CommonConstants.SUCCESS){
                        String key=CommonConstants.SWITCH_CREATE_ROOM_BREAKFAST+switchCreateRoomEntity.getMtHotelId()+"_"+switchCreateRoomEntity.getMtGoodId()+"_1";
                        if(redisService.exists(key)){
                            redisService.remove(key);
                        }
                        key=CommonConstants.SWITCH_CREATE_ROOM_BREAKFAST+switchCreateRoomEntity.getMtHotelId()+"_"+switchCreateRoomEntity.getMtGoodId()+"_0";
                        if(redisService.exists(key)){
                            redisService.remove(key);
                        }
                        //2.添加到产品失效库
                        SwitchCreateRoomFailureEntity switchCreateRoomFailureEntity = new SwitchCreateRoomFailureEntity();
                        BeanUtils.copyProperties(switchCreateRoomEntity, switchCreateRoomFailureEntity);
                        switchCreateRoomFailureEntity.setId(switchCreateRoomFailureEntity.getId() + StringUtil.getFourRandom());
                        int flag = switchCreateRoomFailureMapper.insert(switchCreateRoomFailureEntity);
                        if (flag > 0) {
                            //3.从产品库删除
                            log.info("=======mtHotelId的值为{},switchCreateRoomEntity.getMtGoodId()的值为{}",mtHotelId,switchCreateRoomEntity.getMtGoodId());
                            switchCreateRoomMapper.deleteRoomInfoByMtHotelIdAndMtRoomIdAndMtGoodId(mtHotelId, mtHotelRoomDeatilVo.getMtRoomId(),String.valueOf(switchCreateRoomEntity.getMtGoodId()));
                            //4.从缓存中清除
                            String keyType = CommonConstants.SWITCH_SELLING_THE_HOUSE_TYPE + switchCreateRoomEntity.getMtHotelId() + "_" + switchCreateRoomEntity.getMtGoodId();
                            if(redisService.exists(keyType)){
                                redisService.remove(keyType);
                            }
                            keyType = CommonConstants.SWITCH_SELLING_THE_HOUSE_ROOM_TYPE + switchCreateRoomEntity.getMtHotelId() + "_" + switchCreateRoomEntity.getMtRoomId();
                            if(redisService.exists(keyType)){
                                redisService.remove(keyType);
                            }
                        }
                    }
                }
                //5.删除效验房型库的映射关系
                String url=CommonUrlConstants.COMPARE_HOST+CompareRequestUrlEnums.REMOVE_HOTEL_BASIC_ROOM_INFO_BY_MT_HOTEL_ID_AND_MT_ROOM_ID.getUrl();
                Map<String,Integer> params=new HashMap<>();
                params.put("mtHotelId",mtHotelId);
                params.put("mtRoomId",mtHotelRoomDeatilVo.getMtRoomId());
                Result<Object> result=switchRestTempaleUtilsService.sendPostRequestToCompareToRemoveRoomInfo(url,params);
                String key = CommonConstants.SWITCH_SELLING_THE_HOUSE_ROOM_TYPE + mtHotelId + "_" + mtHotelRoomDeatilVo.getMtRoomId();
                if(redisService.exists(key)){
                    redisService.remove(key);
                }
                switchRoomFailureLogEntity.setCreateTime(new Date());
                switchRoomFailureLogEntity.setGenre(mtHotelRoomDeatilVo.getGenre());
                switchRoomFailureLogEntity.setModifyTime(new Date());
                switchRoomFailureLogEntity.setMtRoomId(mtHotelRoomDeatilVo.getMtRoomId());
                switchRoomFailureLogEntity.setRemark("");
                switchRoomFailureLogMapper.insert(switchRoomFailureLogEntity);
                log.info("=========请求效验房型删除房型直接的映射关系成功,返回数据为{}================",result);
            }
            String url=CommonUrlConstants.COMPARE_HOST+CompareRequestUrlEnums.COMPARE_HOTEL_ROOM_BY_HOTEL_ID_SQL.getUrl();
            Map<String,Integer> params=new HashMap<>();
            params.put("mtHotelId",mtHotelId);
            params.put("line",2);
            switchRestTempaleUtilsService.sendPostRequestToCompareToCompareHotelRoomId(url,params);
        }
    }
}
